String.split() not behaving in android

1.2k views Asked by At

I have a code in Java that is meant to get the server info from a Minecraft server. This returns data split by §. In eclipse, when run as an application, the code works fine. The issue is when I bring it to Android. I have done some research on this but haven't been able to find a working solution. (Have tried Pattern.quote("§")) Here is an example of what I'm running:

String input = "Look like this§0§25";
String[] data = input.split("§");

The expected data would be a 3-long String[] with the values "Look like this", "0", and "25". This is what happens in eclipse. In android, I get a 1-long String[] with the value "Look like this§0§25". Does anyone know if this is an issue with android or am I doing something wrong?

1

There are 1 answers

0
Dorukhan Arslan On BEST ANSWER

It is clearly an encoding issue. I copied the string directly from your question and pasted in Android Studio. Then, run this code segment:

String input = "Look like this§0§25";
String[] data = input.split("§");

for (int i = 0; i < data.length; i++)
    Log.v("data[" + i + "] ->", data[i]);

The output was what you ask:

V/data[0] ->﹕ Look like this
V/data[1] ->﹕ 0
V/data[2] ->﹕ 25

Alternatively, you may use StringTokenizer as follows:

StringTokenizer tokens = new StringTokenizer(input, "§");
String nextToken = tokens.nextToken();

However, if you do not match the character codes properly, both methods will not work.