-1

String "aaaaaaaa|bbbbbbbbb|cccccccc|dddddddddd|"

There is code

 for (int i=0; i<4; i++){
        mas[i]=str.substring(i,str.indexOf("|",i) );
 }

How to get an array of strings by separating the symbol '|'?

SORRY, HOW to ADD CODE in Comment? in StackOverlow?

frodopusto
  • 37
  • 6
  • 2
    is this for Java or C++ ? Please remove the tag that doesn't apply. – Sander De Dycker Feb 27 '15 at 10:41
  • read [Markdown Help](http://stackoverflow.com/editing-help) – Cataclysm Feb 27 '15 at 10:54
  • similar question http://stackoverflow.com/questions/17148150/stringtokenizer-and-string-split-split-on-special-character – Cataclysm Feb 27 '15 at 10:55
  • possible duplicate of [How to split a String in Java](http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – BiscuitBaker Feb 27 '15 at 10:57
  • @BiscuitBaker, duplicate of http://stackoverflow.com/questions/17148150/stringtokenizer-and-string-split-split-on-special-character. http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java Not Duplicate! – frodopusto Feb 27 '15 at 11:13
  • SORRY for DUplicate, I just learned how to use the site , do not judge strictly !!! – frodopusto Feb 27 '15 at 11:14

2 Answers2

5

Try using String's split method like:

String str = "aaaaaaaa|bbbbbbbbb|cccccccc|dddddddddd";
String[] mas=str.split("\\|");
System.out.println(Arrays.toString(mas));
Output:
[aaaaaaaa, bbbbbbbbb, cccccccc, dddddddddd]
SMA
  • 36,381
  • 8
  • 49
  • 73
2

In Java:

String myString = "asdasda|asdasdadasda|dfsfsdff|sdfsdfsdfsfd|";
String[] arrayOfStrings = myString.split("\\|");
HSquirrel
  • 839
  • 4
  • 16
  • you need \\ to split with special chars – Cataclysm Feb 27 '15 at 10:41
  • not working(( String myString = "asdasda|asdasdadasda|dfsfsdff|sdfsdfsdfsfd|"; String[] arrayOfStrings = myString.split("|"); for (String asd: arrayOfStrings){ Log.e("asdasd",asd); } } – frodopusto Feb 27 '15 at 10:44
  • Thanks Cataclysm. I could have sworn I used it a few days ago like that. But I checked it and you're correct. Serves me right for not testing first :P. – HSquirrel Feb 27 '15 at 10:47
  • frodopusto: as Cataclysm and SMA indicated, I needed to add \\ before the |. This is because split in java expects a regular expression (and | is a special character). – HSquirrel Feb 27 '15 at 10:49