Ruby string


Release date:2023-10-28 Update date:2023-10-31 Editor:admin View counts:345

Label:

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-quote strings (’) and double-quote strings (“). The difference is that double-quote strings can support more escape characters.

Single quotation mark string

The simplest string is the single quote string, which stores the string within the single quote:

'This is a string from a Ruby program'

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:

'Won\\'t you read O\\'Reilly\\'s book?'

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.

Double quotation mark string

In a double quote string, we can use the #{} pound sign and curly braces to evaluate the value of the expression:

Embed variables in the string:

Example

#!/usr/bin/ruby#-*- coding: UTF-8 -*-name1="Joe"name2="Mary"puts"hello
#{name1}, #{name2} where?"

The output output of the above example is as follows:

Hello Joe, where is Mary?

Perform mathematical operations in a string:

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 output output of the above example is as follows:

The value of x is 12
The value of x+y is 48
The average value of x+y+z is 40

Ruby also supports a %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.

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.

Example

#/ Usr/bin/ruby # - * - coding: UTF-8- * - desc1=% Q {Ruby strings can use ''
And ''.} Desc2=% q | Ruby strings can use '' and ''\| Putsdesc1putsdesc2

The output output of the above example is as follows:

Ruby strings can use '' and ''.
Ruby strings can use '' and ''.

Escape character

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

Character coding

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 $KCODE change the character set, as follows:

$KCODE = 'u'

The following are possible values of $KCODE``.

Coding

Description

A

ASCII (same as none). This is the default.

E

EUC .

N

None (same as ASCII).

U

UTF-8 .

String built-in method

We need an instance of the String object to call the String method. The following is how to create an instance of a String object:

new[String.new(str="")]

This returns a file that contains 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 will produce the following results:

this is test

The following is the common string method (assuming str is a String object):

Serial number

Method & description

1

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.

2

str * integer returns a new string containing integer str. In other words, str is repeated integer times.

3

str + other_str connect other_str to str.

4

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.

5

str <=> other_str compares str to other_str and returns-1 (less than), 0(equal to), or 1 (greater than). Comparisons are case sensitive.

6

str == obj check the equality of str and obj. Returns false if obj is not a string, if str <=> obj returns true and 0.

7

Str = ~ obj matches str based on the regular expression pattern obj. Returnsthe position where the match begins, otherwise returns false.

8

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

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 $/ ),usually \n . If there is no record delimiter, no action is taken.

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

str.concat(other_str) connect other_str to str.

18

str.count(str, ...) count one or more character sets. If there are multiple character sets, the intersection of those sets is counted.

19

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.zA.Z0.9. or / .

20

str.delete(other_str, ...) returns a copy of str, and all characters in the parameter intersection are deleted.

21

str.delete!(other_str, ...) same as delete, but str changes and returns.

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 \nnn symbol, all special characters are escaped.

25

str.each(separator=$/) { |substr| block } use parameters as record delimiters (default is $/ separates the str, passing each substring to the supplied block.

26

str.each_byte { |fixnum| block } pass each byte of str to block and return each byte in decimal representation.

27

str.each_line(separator=$/) { |substr| block } use parameters as record delimiters (default is $/ separates the str, passing each substring to the supplied block.

28

str.empty? if str is empty (that is, the length is 0), true is returned.

29

str.eql?(other) if two strings have the same length and content, the twostrings are equal.

30

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 ).

31

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.

32

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.

33

str.gsub!(pattern, replacement) [or] str.gsub!(pattern) { |match| block } execution String#gsub returns str, or nil if no replacement is performed

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 0x and return the corresponding number Returns zero if there is an error.

36

str.include? other_str [or] str.include? Fixnum returns true if str contains a given string or character.

37

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.

38

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.

39

str.inspect returns a printable version of str with escaped special characters.

40

str.intern [or] str.to_sym returns the symbol corresponding to str, or creates a symbol if it does not exist before.

41

str.length returns the length of the str. Compare it to size.

42

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.

43

str.lstrip returns a copy of str, removing leading spaces.

44

str.lstrip! removes the leading space from the str and returns nil if there is no change.

45

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.

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

str.replace(other_str) replace the contents of the str with other_str the corresponding value.

48

str.reverse returns a new string, which is the reverse order of str.

49

str.reverse! reversing str,str changes and returns.

50

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.

51

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.

52

str.rstrip returns a copy of str, removing trailing spaces.

53

str.rstrip! removes trailing spaces from str and returns nil if there isno change.

54

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.

55

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.

56

str.split(pattern=$;, [limit]) based on the delimiter, str is divided into substrings and an array of those substrings is returned.

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 $ the value of; If $ for nil (the default), str splits based on spaces, as if specified ` ` As adelimiter.

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

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.

58

str.squeeze!([other_str]*) same as squeeze, but str changes and returns,or nil if there is no change.

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

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.

62

str.sub!(pattern, replacement) [or] str.sub!(pattern) { |match| block } execution String#sub replace and return str, or nil if no replacement is performed.

63

str.succ [or] str.next returns the inheritance of str.

64

str.succ! [or] str.next! equivalent to String#succ , but the str changes and returns.

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

str.swapcase! is equivalent to String#swapcase , but the str changes andreturns, or nil if there is no change

68

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.

69

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.

70

str.to_s [or] str.to_str returns the received value.

71

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.

72

str.tr!(from_str, to_str) is equivalent to String#tr , but the str changes and returns, or nil if there is no change

73

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.

74

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

75

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.

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

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.

String unpack instruction

The following table lists the methods String#unpack the decompression instruction.

Instruction

Return

Description

A

String

Remove trailing null and spaces.

A

String

String.

B

String

Extract bits from each character (starting with the most significant bits).

B

String

Extract bits from each character (starting with the least significant bits).

C

Fixnum

Extract a character as an unsigned integer.

C

Fixnum

Extract a character as an integer.

D, d

Float

Treat sizeof (double)-length characters as native double.

E

Float

Treat characters of sizeof (double) length as double of littleendian byte order.

E

Float

Treat characters of sizeof (float) length as float of littleendian byte order.

F, f

Float

Treat sizeof (float)-length characters as native float.

G

Float

Treat characters of sizeof (double) length as double of network byte order.

G

Float

Treat characters of sizeof (float) length as float of network byte order.

H

String

Extract hexadecimal (starting with the most significant bits) from each character.

H

String

Extract hexadecimal from each character (starting with the least significantbits).

I

Integer

Consecutive characters of sizeof (int) length (modified by _) are treated asnative integer.

I

Integer

Consecutive characters of sizeof (int) length (modified by _) are treated assigned native integer.

L

Integer

Treat four consecutive characters (modified by _) as unsigned native long integer.

L

Integer

Treat four consecutive characters (modified by _) as signed native long integer.

M

String

References are printable.

M

String

Base64 coding.

N

Integer

An unsigned long that treats four characters as network byte order.

N

Fixnum

An unsigned short that treats two characters as network byte order.

P

String

Use a character of length sizeof(char *) as a pointer and return the emph{len} character from the referenced position.

P

String

Put sizeof(char *) character is treated as a pointer to a null ending character.

Q

Integer

Treat eight characters as unsigned quad word (64-bit).

Q

Integer

Treat eight characters as signed quad word (64 bits).

S

Fixnum

An unsigned short that treats two consecutive characters (different if using _) as native byte order.

S

Fixnum

A signed short that treats two consecutive characters (different if using _)as native byte order.

U

Integer

The UTF-8 character as an unsigned integer.

U

String

UU coding.

V

Fixnum

An unsigned long that treats four characters as little-endian byte order.

V

Fixnum

An unsigned short that treats two characters as little-endian byte order.

W

Integer

An integer compressed by BER.

X

Skip one character backwards.

X

Skip one character forward.

Z

String

Use * together, remove the trailing null until the first null.

@

Skips the offset given by the length parameter.

Example

Try the following example to extract all kinds of data.

"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"]

Powered by TorCMS (https://github.com/bukun/TorCMS).