種類
- インスタンスメソッド (1949)
- 特異メソッド (39)
キーワード
- % (11)
- * (11)
- + (11)
- +@ (9)
- -@ (9)
- << (11)
- <=> (11)
- == (11)
- === (11)
- =~ (11)
- [] (66)
- []= (77)
-
ascii
_ only? (11) - b (11)
- byteindex (2)
- byterindex (2)
- bytes (22)
- bytesize (11)
- byteslice (33)
- bytesplice (4)
- capitalize (11)
- capitalize! (11)
- casecmp (11)
- casecmp? (8)
- center (11)
- chars (22)
- chomp (11)
- chomp! (11)
- chop (11)
- chop! (11)
- chr (11)
- clear (11)
- codepoints (22)
- concat (19)
- count (11)
- crypt (11)
- dedup (2)
- delete (11)
- delete! (11)
-
delete
_ prefix (7) -
delete
_ prefix! (7) -
delete
_ suffix (7) -
delete
_ suffix! (7) - downcase (11)
- downcase! (11)
- dump (11)
-
each
_ byte (22) -
each
_ char (22) -
each
_ codepoint (22) -
each
_ grapheme _ cluster (14) -
each
_ line (22) - empty? (11)
- encode (33)
- encode! (22)
- encoding (11)
-
end
_ with? (11) - eql? (11)
-
force
_ encoding (11) - getbyte (11)
-
grapheme
_ clusters (14) - gsub (44)
- gsub! (44)
- hash (11)
- hex (11)
- include? (11)
- index (11)
- insert (11)
- inspect (11)
- intern (11)
- length (11)
- lines (22)
- ljust (11)
- lstrip (11)
- lstrip! (11)
- match (22)
- match? (8)
- new (28)
- next (11)
- next! (11)
- oct (11)
- ord (11)
- partition (11)
- prepend (19)
- replace (11)
- reverse (11)
- reverse! (11)
- rindex (11)
- rjust (11)
- rpartition (11)
- rstrip (11)
- rstrip! (11)
- scan (22)
- scrub (33)
- scrub! (33)
- setbyte (11)
- size (11)
- slice (66)
- slice! (66)
- split (17)
- squeeze (11)
- squeeze! (11)
-
start
_ with? (11) - strip (11)
- strip! (11)
- sub (33)
- sub! (33)
- succ (11)
- succ! (11)
- sum (11)
- swapcase (11)
- swapcase! (11)
-
to
_ c (11) -
to
_ f (11) -
to
_ i (11) -
to
_ r (11) -
to
_ s (11) -
to
_ str (11) -
to
_ sym (11) - tr (11)
- tr! (11)
-
tr
_ s (11) -
tr
_ s! (11) -
try
_ convert (11) - undump (7)
-
unicode
_ normalize (10) -
unicode
_ normalize! (10) -
unicode
_ normalized? (10) - unpack (11)
- unpack1 (8)
- upcase (11)
- upcase! (11)
- upto (11)
-
valid
_ encoding? (11)
検索結果
先頭5件
-
String
# gsub!(pattern , hash) -> self | nil (3.0) -
文字列中の pattern にマッチした部分をキーにして hash を引いた値へ破壊的に置き換えます。
文字列中の pattern にマッチした部分をキーにして hash を引いた値へ破壊的に置き換えます。
@param pattern 置き換える文字列のパターン
@param hash 置き換える文字列を与えるハッシュ
//emlist[例][ruby]{
hash = {'b'=>'B', 'c'=>'C'}
str = "abcabc"
str.gsub!(/[bc]/){hash[$&]}
p str #=> "aBCaBC"
str = "abcabc"
str.gsub!(/[bc]/, hash)
p str #=> "aBCaBC"
//... -
String
# gsub!(pattern , replace) -> self | nil (3.0) -
文字列中で pattern にマッチする部分全てを文字列 replace に破壊的に置き換えます。
...列
@return 置換した場合は self、置換しなかった場合は nil
//emlist[例][ruby]{
buf = "String-String"
buf.gsub!(/in./, "!!")
p buf # => "Str!!-Str!!"
buf = "String.String"
buf.gsub!(/in./, '<<\&>>')
p buf # => "Str<<ing>>-Str<<ing>>"
//}
注意:
引数 replace の中で $1......}") # NG
'abbbcd'.gsub!(/a(b+)/, "\1") # NG
'abbbcd'.gsub!(/a(b+)/, "\\1") # OK
'abbbcd'.gsub!(/a(b+)/, '\\1') # OK
'abbbcd'.gsub!(/a(b+)/, '\1') # OK
'abbbcd'.gsub!(/a(b+)/) { $1 } # OK これがもっとも安全
//}
@see String#sub, String#gsub... -
String
# gsub(pattern) -> Enumerator (3.0) -
文字列中で pattern にマッチした部分を順番にブロックに渡し、 その実行結果で置き換えた文字列を生成して返します。 ブロックなしの場合と違い、ブロックの中からは 組み込み変数 $1, $2, $3, ... を問題なく参照できます。
...表現。
文字列を指定した場合は全く同じ文字列にだけマッチする
@return 新しい文字列
//emlist[例][ruby]{
p 'abcabc'.gsub(/[bc]/) {|s| s.upcase } #=> "aBCaBC"
p 'abcabc'.gsub(/[bc]/) { $&.upcase } #=> "aBCaBC"
//}
@see String#sub, String#scan... -
String
# gsub(pattern) {|matched| . . . . } -> String (3.0) -
文字列中で pattern にマッチした部分を順番にブロックに渡し、 その実行結果で置き換えた文字列を生成して返します。 ブロックなしの場合と違い、ブロックの中からは 組み込み変数 $1, $2, $3, ... を問題なく参照できます。
...表現。
文字列を指定した場合は全く同じ文字列にだけマッチする
@return 新しい文字列
//emlist[例][ruby]{
p 'abcabc'.gsub(/[bc]/) {|s| s.upcase } #=> "aBCaBC"
p 'abcabc'.gsub(/[bc]/) { $&.upcase } #=> "aBCaBC"
//}
@see String#sub, String#scan... -
String
# gsub(pattern , hash) -> String (3.0) -
文字列中の pattern にマッチした部分をキーにして hash を引いた値で置き換えます。
文字列中の pattern にマッチした部分をキーにして hash を引いた値で置き換えます。
@param pattern 置き換える文字列のパターン
@param hash 置き換える文字列を与えるハッシュ
//emlist[例][ruby]{
hash = {'b'=>'B', 'c'=>'C'}
p "abcabc".gsub(/[bc]/){hash[$&]} #=> "aBCaBC"
p "abcabc".gsub(/[bc]/, hash) #=> "aBCaBC"
//} -
String
# gsub(pattern , replace) -> String (3.0) -
文字列中で pattern にマッチする部分全てを 文字列 replace で置き換えた文字列を生成して返します。
...可読性を上げるには、
\& や \1 よりも下記のようにブロック付き形式の gsub を使うべきです。
//emlist[][ruby]{
p 'xbbb-xbbb'.gsub(/x(b+)/) { $1 } # => "bbb-bbb" # OK
puts '\n'.gsub(/\\/) { '\\\\' } # => \\n # OK
//}
@see String#sub, String#gsub!... -
String
# hash -> Integer (3.0) -
self のハッシュ値を返します。 eql? で等しい文字列は、常にハッシュ値も等しくなります。
self のハッシュ値を返します。
eql? で等しい文字列は、常にハッシュ値も等しくなります。
//emlist[例][ruby]{
"test".hash # => 4038258770210371295
("te" + "st").hash == "test".hash # => true
//}
@see Hash -
String
# hex -> Integer (3.0) -
文字列に 16 進数で数値が表現されていると解釈して整数に変換します。 接頭辞 "0x", "0X" とアンダースコアは無視されます。 文字列が [_0-9a-fA-F] 以外の文字を含むときはその文字以降を無視します。
...# => 0
p "10z".hex # => 16
p "1_0".hex # => 16
p "".hex # => 0
//}
@see String#oct, String#to_i, String#to_f,
Kernel.#Integer, Kernel.#Float
このメソッドの逆に数値を文字列に変換するには
Kernel.#sprintf, String#%,
Integer#to_s
などを使ってください。... -
String
# include?(substr) -> bool (3.0) -
文字列中に部分文字列 substr が含まれていれば真を返します。
文字列中に部分文字列 substr が含まれていれば真を返します。
@param substr 検索する文字列
//emlist[例][ruby]{
"hello".include? "lo" #=> true
"hello".include? "ol" #=> false
"hello".include? ?h #=> true
//} -
String
# index(pattern , pos = 0) -> Integer | nil (3.0) -
文字列のインデックス pos から右に向かって pattern を検索し、 最初に見つかった部分文字列の左端のインデックスを返します。 見つからなければ nil を返します。
...ンデックス
//emlist[例][ruby]{
p "astrochemistry".index("str") # => 1
p "regexpindex".index(/e.*x/, 2) # => 3
p "character".index(?c) # => 0
p "foobarfoobar".index("bar", 6) # => 9
p "foobarfoobar".index("bar", -6) # => 9
//}
@see String#rindex......mlist[例][ruby]{
p "astrochemistry".index("str") # => 1
p "regexpindex".index(/e.*x/, 2) # => 3
p "character".index(?c) # => 0
p "foobarfoobar".index("bar", 6) # => 9
p "foobarfoobar".index("bar", -6) # => 9
//}
@see String#rindex
@see String#byteindex...