The String object in Ruby is used to store or manipulate a sequence of one or more bytes.
Ruby strings are divided into single-quote strings (’) and double-quote strings (“). The difference is that double-quote strings can support more escape characters. The simplest string is the single quote string, which stores the string within the single quote: If you need to use a single quote character within a single quote string, you need to use a backslash () in the single quote string so that the Rubyinterpreter does not consider the single quote character to be the termination symbol of the string: A backslash can also escape another backslash so that the second backslash itself is not interpreted as an escape character. The following are string-related features in Ruby. In a double quote string, we can use the Embed variables in the string: The output output of the above example is as follows: Perform mathematical operations in a string: The output output of the above example is as follows: Ruby also supports a The character followed by Q or Q is a delimiter. The delimiter can be any single-byte character that is not alphanumeric. For example, [, {, (, <,! The string is read until a matching Terminator is found. The output output of the above example is as follows: The subscript lists escaped or non-print characters that can be escaped using a backslash symbol. Note: escape characters are parsed within a string enclosed in double quotes. Within a string enclosed in single quotation marks, escape characters are not parsed and output as is. Backslash symbol Hexadecimal character Description \a 0x07 Alarm symbol \b 0x08 Backspace key \ cx Control-x \ Cmurx Control-x \e 0x1b Escape character \f 0x0c Feed character \ M -Cmurx Meta-Control-x \n 0x0a Newline character \nnn Octal representation, where the range of n is 0.7 \r 0x0d Carriage return symbol \s 0x20 Space character \t 0x09 Tab character \v 0x0b Vertical tab character \x Character x \xnn Hexadecimal representation where the range of n is 0.9, a.f, or A.F The default character set for Ruby is ASCII, and characters can be represented by a single byte. If you use UTF-8 or other modern character sets, characters may be represented by one to four bytes. You can use the The following are Coding 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 the This returns a file that contains This will produce the following results: The following is the common string method (assuming Serial number Method & description 1 2 3 4 5 6 7 Str = ~ obj matches str based on the regular expression pattern obj. Returnsthe position where the match begins, otherwise returns false. 8 9 str.capitalize converts the first letter of a string to uppercase and the rest to lowercase. 10 str.capitalize! Same as capitalize, but if not modified, capitalize! Return to nil. 11 str.casecmp is a case-insensitive string comparison. 12 str.center centers the string. 13 Str.chomp removes the record delimiter from the end of the string 14 str.chomp! Same as chomp, but str changes and returns. 15 str.chop removes the last character from the str. 16 str.chop! Same as chop, but str changes and returns. 17 18 19 20 21 22 str.downcase returns a copy of str, and all uppercase letters are replaced with lowercase letters. 23 str.downcase! Same as downcase, but str changes and returns. 24 str.dump returns the version of str, and all non-print characters are replaced with 25 26 27 28 29 30 31 32 33 34 str.hash returns a hash based on the length and content of the string. 35 str.hex treats the leading character of a str as a string of hexadecimal numbers (an optional symbol and an optional 36 37 38 39 str.inspect returns a printable version of str with escaped special characters. 40 41 42 43 44 45 46 Str.oct treats the leading character of str as a string of decimal numbers (an optional symbol) and returns the corresponding number. If the conversionfails, 0 is returned. 47 48 49 50 51 52 53 54 55 56 If pattern is a string String, it will be used as a delimiter when splittingthe str. If pattern is a single space, then str is split based on spaces, ignoring leading spaces and consecutive space characters. If pattern is a regular expression Regexp, the str is split where the pattern matches. When pattern matches a zero-length string, the str is splitinto a single character. If the pattern parameter is omitted, use the If the limit parameter is omitted, the trailing null field is suppressed. Iflimit is a positive number, the maximum number of fields is returned (if limit is 1, the entire string is returned as the only entry in the 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 58 59 Str.strip returns a copy of str, removing leading and trailing spaces. 60 Str.strip! Removes leading and trailing spaces from str, and returns nil if there is no change. 61 62 63 64 65 Str.sum (n = 16) returns the n-bit checksum of the characters in str, where n is the optional Fixnum parameter and defaults to 16. The result is to simply sum the binary values of each character in the str, using 2n-1 as themodule. This is not a very good checksum. 66 Str.swapcase returns a copy of str, with all uppercase letters converted to lowercase letters and all lowercase letters converted to uppercase letters. 67 68 69 70 71 72 73 74 75 76 Str.upcase returns a copy of str, and all lowercase letters are replaced with uppercase letters. The operation is environment-insensitive, and only the characters a to z are affected. 77 Str.upcase! Change the content of str to uppercase, and return nil if there is no change. 78 The following table lists the methods Instruction Return Description A Remove trailing null and spaces. A String. B Extract bits from each character (starting with the most significant bits). B Extract bits from each character (starting with the least significant bits). C Extract a character as an unsigned integer. C Extract a character as an integer. D, d Treat sizeof (double)-length characters as native double. E Treat characters of sizeof (double) length as double of littleendian byte order. E Treat characters of sizeof (float) length as float of littleendian byte order. F, f Treat sizeof (float)-length characters as native float. G Treat characters of sizeof (double) length as double of network byte order. G Treat characters of sizeof (float) length as float of network byte order. H Extract hexadecimal (starting with the most significant bits) from each character. H Extract hexadecimal from each character (starting with the least significantbits). I Consecutive characters of sizeof (int) length (modified by _) are treated asnative integer. I Consecutive characters of sizeof (int) length (modified by _) are treated assigned native integer. L Treat four consecutive characters (modified by _) as unsigned native long integer. L Treat four consecutive characters (modified by _) as signed native long integer. M References are printable. M Base64 coding. N An unsigned long that treats four characters as network byte order. N An unsigned short that treats two characters as network byte order. P Use a character of length P Put Q Treat eight characters as unsigned quad word (64-bit). Q Treat eight characters as signed quad word (64 bits). S An unsigned short that treats two consecutive characters (different if using _) as native byte order. S A signed short that treats two consecutive characters (different if using _)as native byte order. U The UTF-8 character as an unsigned integer. U UU coding. V An unsigned long that treats four characters as little-endian byte order. V An unsigned short that treats two characters as little-endian byte order. W An integer compressed by BER. X Skip one character backwards. X Skip one character forward. Z Use @ Skips the offset given by the length parameter. Try the following example to extract all kinds of data. 6.21.1. Single quotation mark string #
'This is a string from a Ruby program'
'Won\\'t you read O\\'Reilly\\'s book?'
6.21.2. Double quotation mark string #
#{}
pound sign and curly braces to evaluate the value of the expression:Example #
#!/usr/bin/ruby#-*- coding: UTF-8 -*-name1="Joe"name2="Mary"puts"hello
#{name1}, #{name2} where?"
Hello Joe, where is Mary?
Example #
#!/usr/bin/ruby#-*- coding: UTF-8 -*-x,y,z=12,36,72puts"The value of x is #{ x
}"puts"x + the value of y is #{ x + y }"puts"x + y + z the average value of is #{ (x + y +
z)/3 }"
The value of x is 12
The value of x+y is 48
The average value of x+y+z is 40
%q
and
%Q
to boot the string variable
%q
single quotation mark reference rules are used, while
%Q
is a doublequotation mark quotation rule, followed by one. The sum of the
(!
[
{
beginning delimiters of, etc. The end delimiter of
}
]
)
, etc.Example #
#/ Usr/bin/ruby # - * - coding: UTF-8- * - desc1=% Q {Ruby strings can use ''
And ''.} Desc2=% q | Ruby strings can use '' and ''\| Putsdesc1putsdesc2
Ruby strings can use '' and ''.
Ruby strings can use '' and ''.
6.21.3. Escape character #
6.21.4. Character coding #
$KCODE
change the character set, as follows:$KCODE = 'u'
possible
values
of $KCODE``. 6.21.5. String built-in method #
String
object to call the
String
method. The following is how to create an instance of a
String
object:new[String.new(str="")]
str
new string object for the copy.Now, use the
str
object, we can call any available instance method. For example:Example #
#!/usr/bin/rubymyStr=String.new("THIS IS
TEST")foo=myStr.downcaseputs"#{foo}"
this is test
str
is a
String
object):
str
%
arg
format the string using the format specification. If arg contains more than one alternative, then arg must be an array. For more information about the format specification, see sprintf under Kernel Modules.
str
*
integer
returns a new string containing integer str. In other words, str is repeated integer times.
str
+
other_str
connect
other_str
to str.
str
<<
obj
concatenate an object to a string. If the object is a fixed number Fixnum with a range of 0.255, it is converted to a character. Compareit to concat.
str
<=>
other_str compares str to other_str and returns-1 (less than), 0(equal to), or 1 (greater than). Comparisons are case sensitive.
str
==
obj
check the equality of str and obj. Returns false if obj is not a string, if
str
<=>
obj
returns true and 0.
str[position]
# Note that ASCII codes are returned instead of characters
str[start,
length]
str[start..end]
str[start...end]
use the index to intercept substrings
$/
),usually
\n
. If there is no record delimiter, no action is taken.
str.concat(other_str)
connect
other_str
to str.
str.count(str,
...)
count one or more character sets. If there are multiple character sets, the intersection of those sets is counted.
str.crypt(other_str)
apply an one-way encrypted hash to str. The parameter is a two-character string with a range of each character
a.z
、
A.Z
、
0.9
、
.
or
/
.
str.delete(other_str,
...)
returns a copy of str, and all characters in the parameter intersection are deleted.
str.delete!(other_str,
...)
same as delete, but str changes and returns.
\nnn
symbol, all special characters are escaped.
str.each(separator=$/)
{
|substr|
block
}
use parameters as record delimiters (default is
$/
separates the str, passing each substring to the supplied block.
str.each_byte
{
|fixnum|
block
}
pass each byte of str to block and return each byte in decimal representation.
str.each_line(separator=$/)
{
|substr|
block
}
use parameters as record delimiters (default is
$/
separates the str, passing each substring to the supplied block.
str.empty?
if str is empty (that is, the length is 0), true is returned.
str.eql?(other)
if two strings have the same length and content, the twostrings are equal.
str.gsub(pattern,
replacement)
[or]
str.gsub(pattern)
{
|match|
block
}
returns a copy of str, and all occurrences of pattern are replaced with the value of replacement or block. Pattern is usually a regular expression Regexp;. If it is a string String, no regular expression metacharacters are interpreted (that is,
/\d/
will match a number, but
\d
Will match a backslash followed by a
d
).
str[fixnum]
[or]
str[fixnum,fixnum]
[or]
str[range]
[or]
str[regexp]
[or]
str[regexp,
fixnum]
[or]
str[other_str]
use the following parameter to reference str: if the parameter is a Fixnum, the character encoding of fixnum is returned; if the parameter is two Fixnum, a substring from the offset (the first fixnum) to the length (the second fixnum) is returned; if the parameter is range, a substring in the range is returned; if the parameter is regexp, the part of the matching string is returned; if the parameter is regexp with fixnum, the matching data of the fixnum position isreturned Parameter is other_str, a substring that matches other_str is returned. A negative Fixnum starts from the end of the string
-1
start.
str[fixnum]
=
fixnum
[or]
str[fixnum]
=
new_str
[or]
str[fixnum,
fixnum]
=
new_str
[or]
str[range]
=
aString
[or]
str[regexp]
=new_str
[or]
str[regexp,
fixnum]
=new_str
[or]
str[other_str]
=
new_str
]
Replace the entire string or part of the string. With slice! Synonym.
str.gsub!(pattern,
replacement)
[or]
str.gsub!(pattern)
{
|match|
block
}
execution
String#gsub
returns str, or nil if no replacement is performed
0x
and return the corresponding number Returns zero if there is an error.
str.include?
other_str
[or]
str.include?
Fixnum
returns true if str contains a given string or character.
str.index(substring
[,
offset])
[or]
str.index(fixnum
[,
offset])
[or]
str.index(regexp
[,
offset])
returns the index of the first occurrence of a given substring, character (fixnum), or pattern (regexp) in str. Returns nil if it is not found. If a second parameter is provided, specify where to start the search in the string.
str.insert(index,
other_str)
insert other_str before the characters of agiven index to modify the str. The negative index is counted from the end of the string and inserted after a given character. The intention is to start inserting a string at the given index.
str.intern
[or]
str.to_sym
returns the symbol corresponding to str, or creates a symbol if it does not exist before.
str.length
returns the length of the str. Compare it to size.
str.ljust(integer,
padstr='
')
if integer is greater than the length of str, a new string of length integer is returned, which is left-aligned with str and populated with padstr. Otherwise, return str.
str.lstrip
returns a copy of str, removing leading spaces.
str.lstrip!
removes the leading space from the str and returns nil if there is no change.
str.match(pattern)
if pattern is not a regular expression, convert pattern to the regular expression Regexp, and then invoke its matching method in str.
str.replace(other_str)
replace the contents of the str with
other_str
the corresponding value.
str.reverse
returns a new string, which is the reverse order of str.
str.reverse!
reversing str,str changes and returns.
str.rindex(substring
[,
fixnum])
[or]
str.rindex(fixnum
[,
fixnum])
[or]
str.rindex(regexp
[,
fixnum])
returns the index of the last occurrence of a given substring, character (fixnum), or pattern (regexp) in str. Returns nil if it is not found. If a second parameter is provided, specify where to end the search in the string. Characters beyond this point will not be considered.
str.rjust(integer,
padstr='
')
if integer is greater than the length of str, a new string of length integer is returned, which is right-aligned withstr and populated with padstr. Otherwise, return str.
str.rstrip
returns a copy of str, removing trailing spaces.
str.rstrip!
removes trailing spaces from str and returns nil if there isno change.
str.scan(pattern)
[or]
str.scan(pattern)
{
|match,
...|
block
}
two forms match pattern (which can be a regular expression Regexp or a string String) to traverse str. For each match, a result is generated, whichis added to the result array or passed to block. If the pattern does not contain a grouping, each independent result consists of a matching string,
$&
composition. If the pattern contains groupings, each independent result is an array containing each grouping entry.
str.slice(fixnum)
[or]
str.slice(fixnum,
fixnum)
[or]
str.slice(range)
[or]
str.slice(regexp)
[or]
str.slice(regexp,
fixnum)
[or]
str.slice(other_str)
See
str[fixnum]
, etc.
str.slice!(fixnum)
[or]
str.slice!(fixnum,
fixnum)
[or]
str.slice!(range)
[or]
str.slice!(regexp)
[or]
str.slice!(other_str)
deletes the specified part from the str and returns the deleted part. If the value is out of rangeand the parameter takes the form of Fixnum, an IndexError is generated. If the parameter is in the form of range, a RangeError will be generated, and the parameter will be in the form of Regexp and String, and the execution action will be ignored.
str.split(pattern=$;,
[limit])
based on the delimiter, str is divided into substrings and an array of those substrings is returned.
$
the value of; If
$
for nil (the default), str splits based on spaces, as if specified ` ` As adelimiter.
str.squeeze([other_str]*)
use the program described for String#count to create a series of characters from the other_str parameter. Returns a new string in which the same character that appears in the collection is replaced with a single character. If no argument is given, all the same characters are replaced with a single character.
str.squeeze!([other_str]*)
same as squeeze, but str changes and returns,or nil if there is no change.
str.sub(pattern,
replacement)
[or]
str.sub(pattern)
{
|match|
block
}
returns a copy of str, and the first occurrence of pattern is replaced with the value of replacement or block. Pattern is usually a regular expression Regexp;. If it is a string String, no regular expression metacharacters are interpreted.
str.sub!(pattern,
replacement)
[or]
str.sub!(pattern)
{
|match|
block
}
execution
String#sub
replace and return str, or nil if no replacement is performed.
str.succ
[or]
str.next
returns the inheritance of str.
str.succ!
[or]
str.next!
equivalent to
String#succ
, but the str changes and returns.
str.swapcase!
is equivalent to
String#swapcase
, but the str changes andreturns, or nil if there is no change
str.to_f
returns the result of interpreting leading characters in str asfloating-point numbers. Extra characters beyond the end of a valid number are ignored. If there is no valid number at the beginning of the str, 0 is returned. This method does not generate an exception.
str.to_i(base=10)
returns the result of interpreting leading characters in str as integer cardinality (cardinality 2, 8, 10, or 16). Extra characters beyond the end of a valid number are ignored. If there is no valid number at the beginning of the str, 0 is returned. This method does not generate an exception.
str.to_s
[or]
str.to_str
returns the received value.
str.tr(from_str,
to_str)
return a copy of str and replace the charactersin from_str with the corresponding characters in to_str. If to_str is shorter than from_str, it is populated with the last character. Both strings can be used
c1.c2
the symbol represents the range of characters. If
from_str
To
^
at the beginning, it represents all characters exceptthose listed.
str.tr!(from_str,
to_str)
is equivalent to
String#tr
, but the str changes and returns, or nil if there is no change
str.tr_s(from_str,
to_str)
according to the str
String#tr
the described rules are processed, and then the repetitive characters that affect the translation are removed.
str.tr_s!(from_str,
to_str)
is equivalent to
String#tr_s
, but the str changes and returns, or nil if there is no change
str.unpack(format)
decode the str (which may contain binary data) based on the format string and return an array of each value extracted. The formatcharacter consists of a series of single-character instructions. Each instruction can be followed by a number indicating the number of times the instruction is repeated. Asterisk (
*
All remaining elements will be used The instruction sSiIlL may be followed by an underscore (
_
use the local size of the underlying platform for the specified type, otherwise use a consistent size independent of the platform Spaces in the format string are ignored.
str.upto(other_str)
{
|s|
block
}
iterate through consecutive values, starting with str, ending with other_str (inclusive), and passing each valueto block in turn. The String#succ method is used to generate each value. 6.21.6. String unpack instruction #
String#unpack
the decompression instruction.
String
String
String
String
Fixnum
Fixnum
Float
Float
Float
Float
Float
Float
String
String
Integer
Integer
Integer
Integer
String
String
Integer
Fixnum
String
sizeof(char
*)
as a pointer and return the emph{len} character from the referenced position.
String
sizeof(char
*)
character is treated as a pointer to a null ending character.
Integer
Integer
Fixnum
Fixnum
Integer
String
Fixnum
Fixnum
Integer
String
*
together, remove the trailing null until the first null. 6.21.7. Example #
"abc\\0\\0abc\\0\\0".unpack('A6Z6')#=> ["abc", "abc
"]"abc\\0\\0".unpack('a3a3')#=> ["abc", "
\\000\000"]"abc\\0abc\\0".unpack('Z*Z*')#=> ["abc ", "abc
"]"aa".unpack('b8B8')#=> ["10000110",
"01100001"]"aaa".unpack('h2H2c')#=> ["16", "61",
97]"\\xfe\\xff\\xfe\\xff".unpack('sS')#=> [-2,
65534]"now=20is".unpack('M*')#=> ["now
is"]"whole".unpack('xax2aX2aX1aX2a')#=> ["h", "e", "l", "l", "o"]