String.split() Issue

Might be Network related cause the String is from a file from a website but i guess the error is general so i post this here:

Code:

System.out.println(line);
String[] part = line.split("|");
System.out.println(part[0]);
System.out.println(part[1]);

Gives me the following output in the Console:

String contains the URL and the File where it should be placed (For a Launcher that can Update my Game) but since splitting the String in those 2 parts doesn’t work well :slight_smile: any idea why it’s doing this?

I had some kind of a problem with splitting strings as well. There are some problems when you try to split URL. It contains characters that tell Java something it shouldn’t. I would just recommend you to implement your own string split method. It would probably be much faster than to try to overcome java’s string split limitations.

so the problem is that the first part of it is actually an url packed in a String?

URL contains certain characters that mess up java’s string splitting. From my experience at least…

Now I’m totally confused, tested it with some Short other String to see if that works:

  • Hello|World
  • H
  • e

So it Splits every Letter when it should just split arround the “|”?

Maybe try splitting with [icode]|[/icode] to escape it. In regex, it means or, but since there is nothing specified, it splits every letter.

Okay,

\|

is an invalid Escape sequence so that won’t work, i took

|

to make sure i use a letter that doesn’t appear in the url itself, but that didn’t work so I’ll take a spacebar for it, that seems to work and i never saw an URL with a spacebar :slight_smile:

I think it should work with [icode]||[/icode] Tested with regex tester.

String[] part = line.split("\\|");

Using regex to split on 1 char is overkill and has serious overhead. Sadly the Java runtime library doesn’t provide this basic feature, so you’ll have to import Apache StringUtils or write your own.