0

I have this code

String speed_string = "baka baka saka laka";
String[] string_array = speed_string.split(" ");
System.out.println(string_array.length);

and it returns the value of 1 when I run it. Why is that? It seems as if only the first word of the string gets saved.

11
  • 3
    Gives 4 for me. Check if those are really spaces? Commented Feb 25, 2014 at 18:59
  • What you want to do actually? Commented Feb 25, 2014 at 19:01
  • How do I check if those are spaces? They're probably not as you all have pointed out. Writing \\s yields the same result. Commented Feb 25, 2014 at 19:01
  • above code give 4 for me as well Commented Feb 25, 2014 at 19:04
  • 1
    I'm curious as to why this question gets downvoted? To me it's clear and straight to the point. Commented Feb 25, 2014 at 19:09

2 Answers 2

6

Use \\s and update the code as below

       String speed_string = "baka baka saka laka";
       String[] string_array = speed_string.split("\\s");
       System.out.println(string_array.length);
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks I tested this but it yields the same result. Is there a way to check what space is denoted with?
same result mean 4 0r 1 ?
The result I got was 1
' Is there a way to check what space is denoted with' wht it mean ?
I meant that your solution is the most plausible, but since it still doesn't work there must be another symbol for space. Do you know of any else other than s? My mother language is Icelandic so that might have something to do with it.
|
6

Most probably what you think is space (ASCII decimal 32) is not (in your input string).
That would explain perfectly the behavior you're seeing.

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.