[Ruby]splitで文字列を分割する方法
書式
文字列.split()
使用例
#!/usr/bin/ruby
#クラスSampleの定義
class Sample
#初期化
def initialize
@mg = "study"
end
#文字列を出力
def testFunc
p "元の文字列"
res= @mg.split("")
p res
p "for文で文字列を分割"
for ele in res
p ele
end
end
end
#インスタンスを作成
cft = Sample.new
#!/usr/bin/ruby
#クラスSampleの定義
class Sample
#初期化
def initialize
@mg = "study"
end
#文字列を出力
def testFunc
p "元の文字列"
res= @mg.split("")
p res
p "for文で文字列を分割"
for ele in res
p ele
end
end
end
#インスタンスを作成
cft = Sample.new
#!/usr/bin/ruby #クラスSampleの定義 class Sample #初期化 def initialize @mg = "study" end #文字列を出力 def testFunc p "元の文字列" res= @mg.split("") p res p "for文で文字列を分割" for ele in res p ele end end end #インスタンスを作成 cft = Sample.new
結果
“元の文字列"
[“s", “t", “u", “d", “y"]
“for文で文字列を分割"
“s"
“t"
“u"
“d"
“y"