YouTip LogoYouTip

Ruby String

The String object in Ruby is used to store or manipulate a sequence of one or more bytes. Ruby strings are divided into single-quoted strings (') and double-quoted strings ("). The difference is that double-quoted strings support more escape characters. ### Single-Quoted Strings The simplest strings are single-quoted strings, which are strings enclosed in single quotes: 'This is a string in a Ruby program' If you need to use a single quote character within a single-quoted string, you need to use a backslash () in the single-quoted string. This way, the Ruby interpreter will not consider this single quote character as the string terminator: 'Won't you read O'Reilly's book?' A backslash can also escape another backslash, so the second backslash itself is not interpreted as an escape character. Below are the string-related features in Ruby. ### Double-Quoted Strings In double-quoted strings, we can use **#{}** (hash and curly braces) to evaluate expressions: Embedding variables in a string: ## Example name1 = "Joe"name2 = "Mary"puts"Hello #{name1}, where is #{name2}?" [Try it Β»](#) The output of the above example is: Hello Joe, where is Mary? Performing mathematical operations in a string: ## Example x, y, z = 12, 36, 72 puts"The value of x is #{ x }"puts"The value of x + y is #{ x + y }"puts"The average of x + y + z is #{ (x + y + z)/3 }" [Try it Β»](#) The output of the above example is: The value of x is 12 The value of x + y is 48 The average of x + y + z is 40 Ruby also supports string variables introduced by %q and %Q. %q uses single-quote rules, while %Q uses double-quote rules, followed by a starting delimiter like (! [ { and a matching ending delimiter like } ] ). The character following q or Q is the delimiter. The delimiter can be any single-byte non-alphanumeric character, such as: [ , { , ( , < , !, etc. The string will be read until a matching closing delimiter is found. ## Example desc1 = %Q{Ruby strings can use '' and "".}desc2 = %q|Ruby strings can use '' and "".|puts desc1 puts desc2 [Try it Β»](#) The output of the above example is: Ruby strings can use '' and "".Ruby strings can use '' and "". The following table lists the escape characters or non-printable characters that can be escaped using the backslash symbol. **Note:** Within a double-quoted string, escape characters are parsed. Within a single-quoted string, escape characters are not parsed and are output as-is. | Backslash Symbol | Hexadecimal Character | Description | | --- | --- | --- | | a | 0x07 | Bell (alert) | | b | 0x08 | Backspace | | cx | | Control-x | | C-x | | Control-x | | e | 0x1b | Escape | | f | 0x0c | Formfeed | | M-C-x | | Meta-Control-x | | n | 0x0a | Newline | | nnn | | Octal notation, where n is in the range 0.7 | | r | 0x0d | Carriage return | | s | 0x20 | Space | | t | 0x09 | Tab | | v | 0x0b | Vertical tab | | x | | Character x | | xnn | | Hexadecimal notation, where n is in the range 0.9, a.f, or A.F | The default character set in Ruby is ASCII, where characters can be represented by a single byte. If you use UTF-8 or another modern character set, characters may be represented by one to four bytes. You can change the character set at the beginning of the program using $KCODE, as shown below: $KCODE = 'u' Below are the possible values for $KCODE. | Encoding | Description | | --- | --- | | a | ASCII (same as none). This is the default. | | e | EUC. | | n | None (same as ASCII). | | u | UTF-8. | We need an instance of a String object to call String methods. Here are the ways to create a String object instance: new[String.new(str="")] This returns a new string object containing a copy of _str_. Now, using the _str_ object, we can call any available instance method. For example: ## Example myStr = String.new("THIS IS TEST")foo = myStr.downcase puts"#{foo}" This will produce the following result: this is test Below are the common string methods (assuming str is a String object): | No. | Method & Description | | --- | --- | | 1 | **str % arg** Formats the string using format specifications. If arg contains more than one substitution, then arg must be an array. For more information on format specifications, see sprintf under the "Kernel Module". | | 2 | **str * integer** Returns a new string containing integer copies of str. In other words, str is repeated integer times. | | 3 | **str + other_str** Concatenates other_str to str. | | 4 | **str << obj** Concatenates an object to the string. If the object is a Fixnum between 0 and 255, it is converted to a character. Compare this with concat. | | 5 | **str other_str** Compares str with other_str, returning -1 (less than), 0 (equal), or 1 (greater than). The comparison is case-sensitive. | | 6 | **str == obj** Checks the equality of str and obj. Returns false if obj is not a string, and returns true if str obj returns 0. | | 7 | **str =~ obj** Matches str against the regular expression pattern obj. Returns the position of the start of the match, otherwise returns false. | | 8 | **str # Note: returns the ASCII code, not the character str[start, length] str[start..end] str[start...end]** Extracts a substring using an index. | | 9 | **str.capitalize** Converts the first letter of the string to uppercase and the rest to lowercase. | | 10 | **str.capitalize!** Same as capitalize, but returns nil if no modification was made. | | 11 | **str.casecmp** Case-insensitive string comparison. | | 12 | **str.center** Centers the string. | | 13 | **str.chomp** Removes the record separator (typically n) from the end of the string. Does nothing if there is no record separator. | | 14 | **str.chomp!** Same as chomp, but str is modified and returned. | | 15 | **str.chop** Removes the last character from str. | | 16 | **str.chop!** Same as chop, but str is modified and returned. | | 17 | **str.concat(other_str)** Concatenates other_str to str. | | 18 | **str.count(str, ...)** Counts one or more character sets. If multiple character sets are given, it counts the intersection of those sets. | | 19 | **str.crypt(other_str)** Applies a one-way cryptographic hash to str. The argument is a two-character string, each character in the range a.z, A.Z, 0.9, ., or /. | | 20 | **str.delete(other_str, ...)** Returns a copy of str with all characters in the intersection of the arguments removed. | | 21 | **str.delete!(other_str, ...)** Same as delete, but str is modified and returned. | | 22 | **str.downcase** Returns a copy of str with all uppercase letters replaced with lowercase. | | 23 | **str.downcase!** Same as downcase, but str is modified and returned. | | 24 | **str.dump** Returns a version of str with all non-printable characters replaced with nnn notation, and all special characters escaped. | | 25 | **str.each(separator=$/) { |substr| block }** Splits str using the parameter as the record separator (default is $/), passing each substring to the provided block. | | 26 | **str.each_byte { |fixnum| block }** Passes each byte in str to the block, returning each byte as a decimal number. | | 27 | **str.each_line(separator=$/) { |substr| block }** Splits str using the parameter as the record separator (default is $/), passing each substring to the provided block. | | 28 | **str.empty?** Returns true if str is empty (i.e., its length is 0). | | 29 | **str.eql?(other)** Two strings are equal if they have the same length and content. | | 30 | **str.gsub(pattern, replacement) str.gsub(pattern) { |match| block }** Returns a copy of str with all occurrences of pattern replaced with replacement or the value of the block. pattern is usually a Regexp; if it is a String, no regular expression metacharacters are interpreted (i.e., /d/ will match a digit, but 'd' will match a backslash followed by a 'd'). | | 31 | **str str[fixnum,fixnum] str str str[regexp, fixnum] str** References str using the following arguments: a single Fixnum returns the character encoding at that index; two Fixnums return a substring from the offset (first fixnum) up to the length (second fixnum); a range returns a substring within that range; a regexp returns the part of the string that matches; a regexp with a fixnum returns the match data at the fixnum position; other_str returns the substring that matches other_str. A negative Fixnum starts from the end of the string with -1. | | 32 | **str = fixnum str = new_str str[fixnum, fixnum] = new_str str = aString str =new_str str[regexp, fixnum] =new_str str = new_str ]** Replaces the whole string or a part of it. Synonymous with slice!. | | 33 | **str.gsub!(pattern, replacement) str.gsub!(pattern) { |match| block }** Performs the substitution of String#gsub, returning str, or nil if no substitution was performed. | | 34 | **str.hash** Returns a hash based on the string's length and content. | | 35 | **str.hex** Interprets the leading characters of str as a hexadecimal number (an optional sign and an optional 0x) and returns the corresponding number. Returns zero if there is an error. | | 36 | **str.include? other_str str.include? fixnum** Returns true if str contains the given string or character. | | 37 | **str.index(substring [, offset]) str.index(fixnum [, offset]) str.index(regexp [, offset])** Returns the index of the first occurrence of the given substring, character (fixnum), or pattern (regexp) in str. Returns nil if not found. If a second argument is provided, it specifies the position in the string to start the search. | | 38 | **str.insert(index, other_str)** Inserts other_str before the character at the given index, modifying str. Negative indices count from the end of the string, and insert after the given character. The intent is to insert a string starting at the given index. | | 39 | **str.inspect** Returns a printable version of str, with special characters escaped. | | 40 | **str.intern str.to_sym** Returns the Symbol corresponding to str, creating the symbol if it did not previously exist. | | 41 | **str.length** Returns the length of str. Compare this with size. | | 42 | **str.ljust(integer, padstr=' ')** If integer is greater than the length of str, returns a new string of length integer with str left-justified and padded with padstr. Otherwise, returns str. | | 43 | **str.lstrip** Returns a copy of str with leading whitespace removed. | | 44 | **str.lstrip!** Removes leading whitespace from str, returning nil if no change was made. | | 45 | **str.match(pattern)** If pattern is not a regular expression, converts it to a Regexp, then invokes its match method on str. | | 46 | **str.oct** Interprets the leading characters of str as an octal number (an optional sign) and returns the corresponding number. Returns 0 if the conversion fails. | | 47 | **str.replace(other_str)** Replaces the contents of str with the corresponding values in other_str. | | 48 | **str.reverse** Returns a new string with the characters of str in reverse order. | | 49 | **str.reverse!** Reverses str, modifying and returning it. | | 50 | **str.rindex(substring [, fixnum]) str.rindex(fixnum [, fixnum]) str.rindex(regexp [, fixnum])** Returns the index of the last occurrence of the given substring, character (fixnum), or pattern (regexp) in str. Returns nil if not found. If a second argument is provided, it specifies the position in the string to end the search. Characters beyond that point are not considered. | | 51 | **str.rjust(integer, padstr=' ')** If integer is greater than the length of str, returns a new string of length integer with str right-justified and padded with padstr. Otherwise, returns str. | | 52 | **str.rstrip** Returns a copy of str with trailing whitespace removed. | | 53 | **str.rstrip!** Removes trailing whitespace from str, returning nil if no change was made. | | 54 | **str.scan(pattern) str.scan(pattern) { |match, ...| block }** Both forms iterate over str, matching pattern (which may be a Regexp or a String). For each match, a result is generated, either added to the result array or passed to the block. If the pattern contains no groups, each individual result consists of the matched string, $&. If the pattern contains groups, each individual result is an array containing each group entry. | | 55 | **str.slice(fixnum) str.slice(fixnum, fixnum) str.slice(range) str.slice(regexp) str.slice(regexp, fixnum) str.slice(other_str) See str, etc. str.slice!(fixnum) str.slice!(fixnum, fixnum) str.slice!(range) str.slice!(regexp) str.slice!(other_str)** Deletes the specified portion from str and returns it. If the value is out of range, arguments with Fixnum forms will raise an IndexError. Range forms will raise a RangeError, and Regexp and String forms will ignore the action. | | 56 | **str.split(pattern=$;, )** Splits str into substrings based on a delimiter, returning an array of these substrings. If _pattern_ is a String, it is used as the delimiter when splitting str. If pattern is a single space, str is split on whitespace, ignoring leading whitespace and consecutive whitespace characters. If _pattern_ is a Regexp, str is split where the pattern matches. When the pattern matches a zero-length string, str is split into individual characters. If the _pattern_ argument is omitted, the value of $; is used. If $; is nil (the default), str is split on whitespace as if ` ` were specified as the delimiter. If the _limit_ argument is omitted, trailing null fields are suppressed. If limit is a positive number, at most that number of fields are returned (if limit is 1, the entire string is returned as the sole entry in an array). If limit is a negative number, there is no limit to the number of fields returned, and trailing null fields are not suppressed. | | 57 | **str.squeeze(*)** Builds a set of characters from the other_str parameter(s) using the procedure described for String#count. Returns a new string where runs of the same character are replaced by a single character. If no arguments are given, all runs of identical characters are replaced by a single character. | | 58 | **str.squeeze!(*)** Same as squeeze, but str is modified and returned, or nil if no change was made. | | 59 | **str.strip** Returns a copy of str with leading and trailing whitespace removed. | | 60 | **str.strip!** Removes leading and trailing whitespace from str, returning nil if no change was made. | | 61 | **str.sub(pattern, replacement) str.sub(pattern) { |match| block }** Returns a copy of str with the first occurrence of pattern replaced with replacement or the value of the block. pattern is usually a Regexp; if it is a String, no regular expression metacharacters are interpreted. | | 62 | **str.sub!(pattern, replacement) str.sub!(pattern) { |match| block }** Performs the substitution of String#sub, returning str, or nil if no substitution was performed. | | 63 | **str.succ str.next** Returns the successor to str. | | 64 | **str.succ! str.next!** Equivalent to String#succ, but modifies and returns str. | | 65 | **str.sum(n=16)** Returns a n-bit checksum of the characters in str, where n is an optional Fixnum parameter, defaulting to 16. The result is simply the sum of the binary values of each character in str, modulo 2n - 1. This is not a particularly good checksum. | | 66 | **str.swapcase** Returns a copy of str with all uppercase letters converted to lowercase and vice versa. | | 67 | **str.swapcase!** Equivalent to String#swapcase, but modifies and returns str, or nil if no change was made. | | 68 | **str.to_f** Returns the result of interpreting the leading characters of str as a floating-point number. Extraneous characters past the end of a valid number are ignored. If there is not a valid number at the start of str, 0.0 is returned. This method never raises an exception. | | 69 | **str.to_i(base=10)** Returns the result of interpreting the leading characters of str as an integer base (base 2, 8, 10, or 16). Extraneous characters past the end of a valid number are ignored. If there is not a valid number at the start of str, 0 is returned. This method never raises an exception. | | 70 | **str.to_s str.to_str** Returns the value of the receiver. | | 71 | **str.tr(from_str, to_str)** Returns a copy of str with the characters in from_str replaced with the corresponding characters in to_str. If to_str is shorter than from_str, it is padded with its last character. Both strings can use the c1.c2 notation to denote a range of characters. If from_str begins with ^, it means that all characters not listed are affected. | | 72 | **str.tr!(from_str, to_str)** Equivalent to String#tr, but modifies and returns str, or nil if no change was made. | | 73 | **str.tr_s(from_str, to_str)** Processes a copy of str as described for String#tr, then removes duplicate characters that would affect the translation. | | 74 | **str.tr_s!(from_str, to_str)** Equivalent to String#tr_s, but modifies and returns str, or nil if no change was made. | | 75 | **str.unpack(format)** Decodes str (which may contain binary data) according to the format string, returning an array of extracted values. Format strings are composed of a sequence of single-character directives. Each directive may be followed by a number, indicating how many times to repeat the directive. An asterisk (*) will use up all remaining elements. The directives sSiIlL may each be followed by an underscore (_), to use the underlying platform's native size for the specified type; otherwise, it uses a platform-independent consistent size. Spaces in the format string are ignored. | | 76 | **str.upcase** Returns a copy of str with all lowercase letters replaced with uppercase. The operation is environment-sensitive; only the characters a through z are affected. | | 77 | **str.upcase!** Changes the contents of str to uppercase, returning nil if no change was made. | | 78 | **str.upto(other_str) { |s| block }** Iterates through successive values, starting at str and ending at other_str (inclusive), passing each value to the block. The String#succ method is used to generate each value. | The following table lists the unpack directives for the method String#unpack. | Directive | Returns | Description | | --- | --- | --- | | A | String | Removes trailing nulls and spaces. | | a | String | String. | | B | String | Extracts bits from each character (first is the most significant bit). | | b | String | Extracts bits from each character (first is the least significant bit). | | C | Fixnum | Extracts a character as an unsigned integer. | | c | Fixnum | Extracts a character as an integer. | | D, d | Float | Treats sizeof(double) characters as a native double. | | E | Float | Treats sizeof(double) characters as a double in little-endian byte order. | | e | Float | Treats sizeof(float) characters as a float in little-endian byte order. | | F, f | Float | Treats sizeof(float) characters as a native float. | | G | Float | Treats sizeof(double) characters as a double in network (big-endian) byte order. | | g | Float | Treats sizeof(float) characters as a float in network (big-endian) byte order. | | H | String | Extracts hex nibbles from each character (first is the most significant). | | h | String | Extracts hex nibbles from each character (first is the least significant). | | I | Integer | Treats sizeof(int) consecutive characters (modified by _) as a native integer. | | i | Integer | Treats sizeof(int) consecutive characters (modified by _) as a signed native integer. | | L | Integer | Treats four (modified by _) consecutive characters as an unsigned native long integer. | | l | Integer | Treats four (modified by _) consecutive characters as a signed native long integer. | | M | String | Quoted-printable. | | m | String | Base64 encoded. | | N | Integer | Treats four characters as an unsigned long in network (big-endian) byte order. | | n | Fixnum | Treats two characters as an unsigned short in network (big-endian) byte order. | | P | String | Treats sizeof(char *) characters as a pointer and returns emph{len} characters from the referenced location. | | p | String | Treats sizeof(char *) characters as a pointer to a null-terminated string. | | Q | Integer | Treats eight characters as an unsigned quad word (64 bits). | | q | Integer | Treats eight characters as a signed quad word (64 bits). | | S | Fixnum | Treats two (different if _ is used) consecutive characters as an unsigned short in native byte order. | | s | Fixnum | Treats two (different if _ is used) consecutive characters as a signed short in native byte order. |
← Ruby ArrayColl Datalist Options β†’