----------------------------------------------------------String#index str.index(substring [, offset]) => fixnum or nil str.index(fixnum [, offset]) => fixnum or nil str.index(regexp [, offset]) => fixnum or nil ----------------------------------------------------------------------- Returns the index of the first occurrence of the given substring, character (fixnum), or pattern (regexp) instr. Returns nil ifnot found. If the second parameter is present, it specifies the position in the stringto begin the search. "hello".index('e') #=> 1 "hello".index('lo') #=> 3 "hello".index('a') #=> nil "hello".index(101) #=>1 "hello".index(/[aeiou]/, -3) #=>4
---------------------------------------------------------String#rindex str.rindex(substring [, fixnum]) => fixnum or nil str.rindex(fixnum [, fixnum]) => fixnum or nil str.rindex(regexp [, fixnum]) => fixnum or nil ----------------------------------------------------------------------- Returns the index of the last occurrence of the given substring, character (fixnum), or pattern (regexp) instr. Returns nil ifnot found. If the second parameter is present, it specifies the position in the stringtoend the search---characters beyond this point will not be considered. "hello".rindex('e') #=> 1 "hello".rindex('l') #=> 3 "hello".rindex('a') #=> nil "hello".rindex(101) #=>1 "hello".rindex(/[aeiou]/, -2) #=>1
------------------------------------------------------------String#Sub str()sub str.sub(pattern, replacement) => new_str str.sub(pattern) {|match| block } => new_str ----------------------------------------------------------------------- Returns a copy ofstrwith the first occurrence of pattern replaced with either replacement or the value of the block. The pattern will typically be a Regexp; if it is a Stringthen no regular expression metacharacters will be interpreted (that is/d/ will match a digit, but 'd' will match a backslash followed by a 'd'). If the method call specifies replacement, special variables such as $& will not be useful, as substitution into the string occurs before the pattern match starts. However, the sequences 1, 2, etc., may be used. In the block form, the current match stringis passed inas a parameter, and variables such as $1, $2, $`, $&, and $' will be set appropriately. The value returned by the block will be substituted for the match on each call. The result inherits any tainting in the original stringor any supplied replacement string. "hello".sub(/[aeiou]/, '*') #=> "h*llo" "hello".sub(/([aeiou])/, '<>') #=> "h<e>llo" "hello".sub(/./) {|s| s[0].to_s +' ' } #=> "104 ello"
-----------------------------------------------------------String#gsub str.gsub(pattern, replacement) => new_str str.gsub(pattern) {|match| block } => new_str ----------------------------------------------------------------------- Returns a copy ofstrwith all occurrences of pattern replaced with either replacement or the value of the block. The pattern will typically be a Regexp; if it is a Stringthen no regular expression metacharacters will be interpreted (that is/d/ will match a digit, but 'd' will match a backslash followed by a 'd'). If a stringis used as the replacement, special variables from the match (such as $&and $1) cannot be substituted into it, as substitution into the string occurs before the pattern match starts. However, the sequences 1, 2, and so on may be used to interpolate successive groups in the match. In the block form, the current match stringis passed inas a parameter, and variables such as $1, $2, $`, $&, and $' will be set appropriately. The value returned by the block will be substituted for the match on each call. The result inherits any tainting in the original stringor any supplied replacement string. "hello".gsub(/[aeiou]/, '*') #=> "h*ll*" "hello".gsub(/([aeiou])/, '<>') #=> "h<e>ll<o>" "hello".gsub(/./) {|s| s[0].to_s +' '} #=> "104 101 108 108 111 "