| Start |
|---|
| Difference between single-quoted and double-quoted String literals The main difference is that double-quoted For instance, they can include arbitrary Ruby expressions via interpolation: Double-quoted strings also support the entire set of escape sequences (opens new window) including ... while single-quoted strings support no escape sequences, baring the minimal set necessary for single-quoted strings to be useful: Literal single quotes and backslashes, |
| Creating a String Ruby provides several ways to create a The main difference is that double-quoted string literals are a little bit more flexible as they support interpolation and some backslash escape sequences. There are also several other possible ways to create a string literal using arbitrary string delimiters. An arbitrary string delimiter is a Finally, you can use the
You can use several different delimiters, as long as there is a matching pair: |
Case manipulation These four methods do not modify the original receiver. For example, There are four similar methods that perform the same actions but modify original receiver. For example, Notes:
|
| String concatenation Concatenate strings with the Or with the Note that the You also can multiply strings, e.g. |
| Splitting a String
An empty A non-matching delimiter results in an You can also split a string using regular expressions: The delimiter is optional, by default a string is split on whitespace: |
| Positioning strings In Ruby, strings can be left-justified, right-justified or centered To left-justify string, use the If the integer is greater than the length of the original string, the new string will be left-justified with the optional string parameter taking the remaining space. If the string parameter is not given, the string will be padded with spaces. To right-justify a string, use the If the integer is greater than the length of the original string, the new string will be right-justified with the optional string parameter taking the remaining space. If the string parameter is not given, the string will be padded with spaces. To center a string, use the |
| Joining Strings
The delimiter is optional, and defaults to an empty An empty |
| String starts with To find if a string starts with a pattern, the You can also check the position of the pattern with |
| String interpolation The double-quoted delimiter |
| String ends with To find if a string ends with a pattern, the |
| Multiline strings The easiest way to create a multiline string is to just use multiple lines between quotation marks: The main problem with that technique is that if the string includes a quotation, it'll break the string syntax. To work around the problem, you can use a heredoc (opens new window) instead: Ruby supports shell-style here documents with Ruby 2.3 solves the problem by introducing Percent Strings (opens new window) also work to create multiline strings: There are a few ways to avoid interpolation and escape sequences: |
| Formatted strings Ruby can inject an array of values into a string by replacing any placeholders with the values from the supplied array. The place holders are represented by two |
| String character replacements The To replace only the first occurrence of a pattern with with another expression use the If you would like to replace all occurrences of a pattern with that expression use To delete characters, pass in an empty string for the second parameter You can also use regular expressions in all these methods. It's important to note that these methods will only return a new copy of a string and won't modify the string in place. To do that, you need to use the |
| Understanding the data in a string In Ruby, a string is just a sequence of bytes (opens new window) along with the name of an encoding (such as Ruby strings can be used to hold text (basically a sequence of characters), in which case the UTF-8 encoding is usually used. Ruby strings can also be used to hold binary data (a sequence of bytes), in which case the ASCII-8BIT encoding is usually used. It is possible for the sequence of bytes in a string to not match the encoding, resulting in errors if you try to use the string. |
String Substitution See String Syntax
|
Read Article
https://develop.horje.com/learn/1434/reference