如何在 Scala 中按字符串拆分字符串 [英] How to split a string by a string in Scala
本文介绍了如何在 Scala 中按字符串拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Ruby 中,我做到了:
In Ruby, I did:
"string1::string2".split("::")
在 Scala 中,我找不到如何使用字符串而不是单个字符来split
.
In Scala, I can't find how to split
using a string, not a single character.
推荐答案
在您的示例中,它没有区别,但是 Scala 中的 String#split
方法实际上采用了 String
表示正则表达式.所以一定要根据需要转义某些字符,例如在 "a..bc".split("""\.\.""")
中,或者为了使这一事实更加明显,您可以在 RegEx
上调用 split 方法>: """\.\.""".r.split("a..bc")
.
In your example it does not make a difference, but the String#split
method in Scala actually takes a String
that represents a regular expression. So be sure to escape certain characters as needed, like e.g. in "a..b.c".split("""\.\.""")
or to make that fact more obvious you can call the split method on a RegEx
: """\.\.""".r.split("a..b.c")
.
这篇关于如何在 Scala 中按字符串拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文