Ruby字符串操作(分隔符)

时间:2012-08-28 13:35:23

标签: ruby

我有一个这样的字符串:

14.015_KNECHT_178178

如何分割它:

art = 14.015
man = KNECHT

如您所见,分隔符为_

2 个答案:

答案 0 :(得分:2)

string#split可以做到这一点。

>> (art,man,foo) = "14.015_KNECHT_178178".split '_'
=> ["14.015", "KNECHT", "178178"]
>> p art
"14.015"
=> "14.015"
>> p man
"KNECHT"
=> "KNECHT"
>> p foo
"178178"
=> "178178"

答案 1 :(得分:2)

试试这个

art,man= "14.015_KNECHT_178178".split(/_/)

了解#split的更多详情