2

Please see the example below. I create a multiline String and then try to split it. It works fine except the first line. By default split method doesn't return delimiters. So, I wonder what is so special about the beginning of the line that it returns "". And how to update the regex to avoid it.

scala> val Shakespear =
     |     """
     |       |To be,
     |       | or not
     |       |to be:
     |     """.stripMargin
Shakespear: String =
"
To be,
 or not
to be:
    "

scala> Shakespear.split("""[\s]+""")
res3: Array[String] = Array("", To, be,, or, not, to, be:)

Thanks.

1 Answer 1

3

Instead of updating regex, how about trim the white spaces at both ends, and then split:

Shakespear.trim.split("\\s+")
// res22: Array[String] = Array(To, be,, or, not, to, be:)

As for why you have an empty string, you have a new line character at the beginning of the string which is also captured by \\s, and splitting on it gives you an empty string since there is nothing in front of the delimiter.

Here is the raw representation of your string (escape borrowed from here):

def escape(raw: String): String = {
    import scala.reflect.runtime.universe._
    Literal(Constant(raw)).toString
}

escape(Shakespear)
// res24: String = "\nTo be,\n or not\nto be:\n          "

If you don't want to split on new line character, use " " instead:

Shakespear.split(" ")

//res26: Array[String] =
//Array("
//To", "be,
//", or, not
//to, "be:
//")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.