Ruby/String/char in string
Материал из Wiki.crossplatform.ru
Accessing Strings
speaker = "King Richard III" # If you enter a string as the argument to [], it will return that string, if found: puts speaker["King"]
Adding another argument, a Fixnum, returns that portion of the matched data, starting at 0 in this instance
line = "A horse! this is a horse!" puts line[/^A horse/, 0]
Change a string with offset
s = "hello" s[0,2] # "he" s[-1,1] # "o": returns a string, not the character code ?o s[0,0] # "": a zero-length substring is always empty s[0,10] # "hello": returns all the characters that are available s[s.length,1] # "": there is an empty string immediately beyond the end s[s.length+1,1] # nil: it is an error to read past that s[0,-1] # nil: negative lengths don"t make any sense
Character literal
?A # Character literal for the ASCII character A ?" # Character literal for the double-quote character ?? # Character literal for the question mark character ?\t # Character literal for the TAB character ?\C-x # Character literal for Ctrl-X ?\111 # Literal for character whose encoding is 0111 (octal)
Concatenate string by string index
string = "My first string" # => "My first string" string[3].chr + string[4].chr + string[5].chr + string[6].chr + string[7].chr
Enter a range to grab a range of characters.
# Two dots (..) means include the last character: cite = "this is a test" cite[0..4]
If you add the chr method from the Integer class, you"ll get the actual character:
line = "A horse! a horse! my kingdom for a horse!" line[7].chr # => "!"
Loop through a string with while
s = "hello" while(s["l"]) # While the string contains the substring "l" s["l"] = "L"; # Replace first occurrence of "l" with "L" end # Now we have "heLLo"
Map chars in a string
p "hello".chars.map {|c| c.succ } # => ["i", "f", "m", "m", "p"]
specify a Fixnum (integer) as an index, it returns the decimal character code for the character found at the index location
line = "A horse! a horse! my kingdom for a horse!" puts line[7] # At the location 7, [] found the character 33 (!).
taking the first letter from a string
"test"[0].chr
use an offset and length (two Fixnums) to tell [] the index location where you want to start, and then how many characters you want to retrieve
line = "A horse! a horse! my kingdom for a horse!" puts line[18, 23] # You started at index location 18, and then scooped up 23 characters from there, inclusive.
use regular expressions
line = "A horse! this is a horse!" puts line[/horse!$/] # The regular expression /horse!$/ asks, "Does the word horse, followed by ! come at the end of the line ($)?" # If this is true, this call returns horse!; nil if not.
Use string as array
s = "hello" puts s[0] # 104: the ASCII character code for the first character "h" puts s[s.length-1] # 111: the character code of the last character "o" puts s[-1] # 111: another way of accessing the last character puts s[-2] # 108: the second-to-last character puts s[-s.length] # 104: another way of accessing the first character puts s[s.length] # nil: there is no character at that index