|
| 1 | +# Concatenating Strings |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +*You will learn:* |
| 6 | +- how to concatenate Strings |
| 7 | +- how to append a String to another String |
| 8 | +- about the ordinal number representation of Strings |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +If you look into Ruby's documentation for the the *String* class you will find several |
| 13 | +methods to concatenate Strings: *(ruby-doc core: String)*. |
| 14 | + |
| 15 | +Adding a String to another String is an often used operation when working with Strings. |
| 16 | +In Ruby there are two possibilities to combine Strings: |
| 17 | + |
| 18 | +`1) You can connect two Strings and create a third String out of them` |
| 19 | +`2) You can directly modify an existing String` |
| 20 | + |
| 21 | +Let's have a closer look into both the options: |
| 22 | + |
| 23 | +## Combining Strings into a new String |
| 24 | + |
| 25 | +A String comes with 2 methods to do this, namely `+` and `\*`. |
| 26 | +You might think "Wait, these are Number operations!". Right, but the *String* class |
| 27 | +does also define them. |
| 28 | + |
| 29 | +The `+` method just adds a second String to the end of the first one: |
| 30 | + |
| 31 | +` 'white' + 'space'` *# => "whitespace"* |
| 32 | + |
| 33 | +If you want to have whitespace between these words you have explicitly add it to |
| 34 | +the end of the first or the beginning of the second String. |
| 35 | + |
| 36 | +The `\*` method takes an Integer *n* and runs the `+`-concatenation *n* times: |
| 37 | + |
| 38 | +` 'la' \* 3` *# => "lalala"* (here n = 3) |
| 39 | + |
| 40 | +If your first String was stored in a variable, then a concatenation with the `+` and `\*` |
| 41 | +methods doesn't change the value of this variable: |
| 42 | + |
| 43 | +` name = 'Ada'` |
| 44 | +` fullname = name + ' Lovelace'` *# => "Ada Lovelace" |
| 45 | +` puts fullname` *# => "Ada Lovelace" |
| 46 | +` puts name` *# => "Ada" |
| 47 | + |
| 48 | +The variable `name` still has the value *"Ada"*, `fullname` holds the concatenated String. |
| 49 | + |
| 50 | +## Directly modifying a String |
| 51 | + |
| 52 | +There are two methods for directly adding a String to another String: `<<` and `concat`. |
| 53 | + |
| 54 | +The `<<` method works like `+` with the difference however that it adds the second String |
| 55 | +to the first one. Let's take the example from above again: |
| 56 | + |
| 57 | +` name = 'Ada'` |
| 58 | +` name << ' Lovelace'` *# => "Ada Lovelace" |
| 59 | +` puts name` *# => "Ada Lovelace" |
| 60 | + |
| 61 | +The value of *name* is now the concatenation. |
| 62 | + |
| 63 | +The method `concat` works the same but just has a more recognizable name: |
| 64 | + |
| 65 | +` name = 'William'` |
| 66 | +` name.concat(' King')` *# => "William King" |
| 67 | +` puts name` *# => "William King" |
| 68 | + |
| 69 | +Both `<<` and `concat` also take an integer number as parameter. |
| 70 | +Hm, how can we add an Integer value to a String? The answer is that each one-character |
| 71 | +String has an Integer representation (ordinal number). You can check which ordinal number |
| 72 | +a character has by using the `ord` method: |
| 73 | + |
| 74 | +` 'A'.ord` *# => 65* |
| 75 | +` '?'.ord` *# => 63* |
| 76 | + |
| 77 | +This means if you write |
| 78 | + |
| 79 | +` 'Pardon'.concat(63)` *# => "Pardon?"* |
| 80 | + |
| 81 | +then the *63* is first converted to its String representation and then added to the first String. |
| 82 | + |
| 83 | +If you want to add a number – as it is – to a String, you have to convert it to a String before. |
| 84 | +You can use the `to_s` method to achive this: |
| 85 | + |
| 86 | +` 'User' << 1.to_s` *# => "User1"* |
| 87 | + |
| 88 | +Now that we learned about all these methods, let's concatenate some Strings! |
| 89 | + |
| 90 | +--- |
| 91 | + |
| 92 | +Create the String *"Ruby rooooocks!"* by using the methods `+`, `\*` and `<<` or `concat`! |
| 93 | + |
| 94 | +Create the whitespace and the exclamation mark by concatenating ordinal numbers to the String. |
| 95 | +Use the `\*` method to add the multiple *ooooo*s to the String. |
| 96 | + |
| 97 | +--- |
0 commit comments