Top > Ruby [2.2.2] > sub , gsub


String# sub , gsub

◆単発

s1 = "achaachaacha"
s2 = s1.sub("acha","porute")
p s1                      # "achaachaacha"
p s2                      # "poruteachaacha"

s1 = "achaachaacha"
s2 = s1.sub(/acha|\+|=/,"piipo")
p s1                      # "achaachaacha"
p s2                      # "piipoachaacha"


◆複数

s1 = "achaachaacha"
s2 = s1.gsub("acha","porute")
p s1                      # "achaachaacha"
p s2                      # "poruteporuteporute"

s1 = "achaachaacha"
s2 = s1.gsub(/acha|\+|=/,"piipo")
p s1                      # "achaachaacha"
p s2                      # "piipopiipopiipo"

これは覚えておいて損は絶対にしません。
誰もがよく使用します。(多分…)



Copyright © 2015 AchaPorutePiipo All Rights Reserved.