1

Please carefully observe these two scenarios.

scala> var aa ="ABCD, aaaa, fdad and others, lkda;fd, fdaf".split(",")

aa: Array[String] = Array(ABCD, " aaaa", " fdad and others", " lkda;fd", " fdaf")

scala> var aa ="ABCD, aaaa, fdad and others, lkda;fd, fdaf".split(" ")

aa: Array[String] = Array(ABCD,, aaaa,, fdad, and, others,, lkda;fd,, fdaf)

in first scenario split based on comma (,) In this scenario first word not showing within "quotes" rest of the words showing within "quotes". Why?

In second scenario separate by comma here everything displaying without "Quotes" Why ? Usually Strings display within a quotes, but here not displaying like that why?

1

1 Answer 1

6

This is not relevant to array. It's the problem of how string literal is displayed on scala REPL. Take a look at the following example:

scala> "hello"
res0: String = hello

scala> "  hello  "
res1: String = "  hello  "

The first string "hello" is displayed as just hello, however, the second is displayed as " hello ". Why? The first string is obvious to recognize without the quotes. But the second contains white spaces before and after hello. Without quotes, it's very hard to recognize that there are white spaces. So it print the string within quotes.

Sign up to request clarification or add additional context in comments.

1 Comment

That makes sense. However the commas in second example are confusing too.

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.