- 2.1 Representing Ordinary Strings
- 2.2 Representing Strings with Alternate Notations
- 2.3 Using Here-Documents
- 2.4 Finding the Length of a String
- 2.5 Processing a Line at a Time
- 2.6 Processing a Byte at a Time
- 2.7 Performing Specialized String Comparisons
- 2.8 Tokenizing a String
- 2.9 Formatting a String
- 2.10 Using Strings As IO Objects
- 2.11 Controlling Uppercase and Lowercase
- 2.12 Accessing and Assigning Substrings
- 2.13 Substituting in Strings
- 2.14 Searching a String
- 2.15 Converting Between Characters and ASCII Codes
- 2.16 Implicit and Explicit Conversion
- 2.17 Appending an Item Onto a String
- 2.18 Removing Trailing Newlines and Other Characters
- 2.19 Trimming Whitespace from a String
- 2.20 Repeating Strings
- 2.21 Embedding Expressions Within Strings
- 2.22 Delayed Interpolation of Strings
- 2.23 Parsing Comma-Separated Data
- 2.24 Converting Strings to Numbers (Decimal and Otherwise)
- 2.25 Encoding and Decoding rot13 Text
- 2.26 Encrypting Strings
- 2.27 Compressing Strings
- 2.28 Counting Characters in Strings
- 2.29 Reversing a String
- 2.30 Removing Duplicate Characters
- 2.31 Removing Specific Characters
- 2.32 Printing Special Characters
- 2.33 Generating Successive Strings
- 2.34 Calculating a 32-Bit CRC
- 2.35 Calculating the MD5 Hash of a String
- 2.36 Calculating the Levenshtein Distance Between Two Strings
- 2.37 Encoding and Decoding base64 Strings
- 2.38 Encoding and Decoding Strings (uuencode/uudecode)
- 2.39 Expanding and Compressing Tab Characters
- 2.40 Wrapping Lines of Text
- 2.41 Conclusion
2.18 Removing Trailing Newlines and Other Characters
Often we want to remove extraneous characters from the end of a string. The prime example is a newline on a string read from input.
The chop method removes the last character of the string (typically a trailing newline character). If the character before the newline is a carriage return (\r), it will be removed also. The reason for this behavior is the discrepancy between different systems' conceptions of what a newline is. On some systems such as UNIX, the newline character is represented internally as a linefeed (\n). On others such as DOS and Windows, it is stored as a carriage return followed by a linefeed (\r\n).
str = gets.chop # Read string, remove newline s2 = "Some string\n" # "Some string" (no newline) s3 = s2.chop! # s2 is now "Some string" also s4 = "Other string\r\n" s4.chop! # "Other string" (again no newline)
Note that the "in-place" version of the method (chop!) will modify its receiver.
It is also important to note that in the absence of a trailing newline, the last character will be removed anyway:
str = "abcxyz" s1 = str.chop # "abcxy"
Because a newline may not always be present, the chomp method may be a better alternative:
str = "abcxyz" str2 = "123\n" str3 = "123\r" str4 = "123\r\n" s1 = str.chomp # "abcxyz" s2 = str2.chomp # "123" # With the default record separator, \r and \r\n are removed # as well as \n s3 = str3.chomp # "123" s4 = str4.chomp # "123"
There is also a chomp! method as we would expect.
If a parameter is specified for chomp, it will remove the set of characters specified from the end of the string rather than the default record separator. Note that if the record separator appears in the middle of the string, it is ignored:
str1 = "abcxyz" str2 = "abcxyz" s1 = str1.chomp("yz") # "abcx" s2 = str2.chomp("x") # "abcxyz"