-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?

4
  • 3
    stackoverflow.com/questions/7021074/… Commented Oct 16, 2015 at 10:59
  • @FrankN.Stein hey this is EXACTLY what i have tried! Commented Oct 16, 2015 at 10:59
  • 2
    @SureshAtta has the perfect answer for you. Commented Oct 16, 2015 at 11:00
  • Using String Tokenizer Commented Oct 16, 2015 at 11:05

1 Answer 1

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("\\|");
Sign up to request clarification or add additional context in comments.

2 Comments

oh that's worked, Thanks alot.
If worked then accept please

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.