-1

I want to split an android string to smaller ones with any | char.

Just imagine I have this long string :

This|is|a|long|string|in|java

So, I wanna split it. I need to get a array in output with this values :

[1]=>"This"
[2]=>"is"
[3]=>"a"
[4]=>"long"
[5]=>"string"
[6]=>"in"
[7]=>"java"

I have tried :

separated = oldstring.split("|");

But, i didn't give me the thing i need!

How can i do that? Any code can do that?

Bana
  • 29
  • 1
  • 6

1 Answers1

6

Note that String's split() method take regex as a param. Not string.

public String[] split(String regex)

Since | is a meta character, and it's have a special meaning in regex.

It works when you escape that.

String separated[]  = oldstring.split("\\|");
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307