0

I have String

val s1 = "dog#$&cat#$&cow#$&snak"
val s2 = s1.split()

how to split string into words

1
  • 4
    Try val s2= s1.split("#\\$&") That will give you s2: Array[String] = Array(dog, cat, cow, snak) Commented Apr 1, 2020 at 14:21

1 Answer 1

2

For a precise split, you could use #\\$& to match all 3 characters where the dollar sign has to be escaped, and the backslash itself also has to be escaped.

val s1= "dog#$&cat#$&cow#$&snak"
val s2= s1.split("#\\$&")

Output

s2: Array[String] = Array(dog, cat, cow, snak)

A broader pattern could be using \\W+ to match 1+ times any character except a word character.

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.