我有一个字符串,说“Hello_World I am Learning,Ruby”。 我想将这个字符串拆分成每个不同的词,最好的方法是什么?
谢谢! C.
最佳答案
您可以将\W 用于任何非单词字符:
"Hello_World I am Learning,Ruby".split /[\W_]/
=> ["Hello", "World", "I", "am", "Learning", "Ruby"]
"Hello_World I am Learning, Ruby".split /[\W_]+/
=> ["Hello", "World", "I", "am", "Learning", "Ruby"]
关于Ruby 字符串拆分为多个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7724135/