How to split a string by a string in Scala
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")
.
The REPL is even easier than Stack Overflow. I just pasted your example as is.
Welcome to Scala version 2.8.1.final (Java HotSpot Server VM, Java 1.6.0_22).
Type in expressions to have them evaluated.
Type :help for more information.
scala> "string1::string2".split("::")
res0: Array[java.lang.String] = Array(string1, string2)