文字列を置換
Top > Ruby [2.2.2] > 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"