The following code:
val sentence = "1 2 3 4".split(" ")
gives me:
Array(1, 2, "", 3, "", "", 4)
but I’d rather want to have only the words:
Array(1, 2, 3, 4)
How can I split the sentence when the words are separated by multiple spaces?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use a regular expression:
The “+” means “one or more of the previous” (previous being a space).
Better yet, if you want to split on all whitespace:
(Where
"\\s"is a Pattern which matches any whitespace. Look here for more examples.)