This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use String.split with "[" separator?

 
Rogerio Kioshi
Ranch Hand
Posts: 690
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

If I have a string like this:
String str="AAAA[BBB[CCC";

And I want to split it in an array:
item[0]="AAAA"
item[1]="BBB"
item[2]="CCC"

How do I do it?

I'm trying
String item[]=str.split("[")

but it's not working...

Seems to be very simple but I don't know. Very shamed!
 
Henry Wong
author
Marshal
Pie
Posts: 21123
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try...



Henry
 
Rogerio Kioshi
Ranch Hand
Posts: 690
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.

(Javadoc API 5 does not explain this very well...)
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it does.

The 1-argument split method links both to the 2-argument split method and Pattern. Moreover, the 2-argument split method not only links to Pattern, but to Pattern.compile in particular. On the first line there's a link to the regular expression summary.

On that last page (part of Pattern), it is clearly mentioned that:
a) [ is used with ] for a set
b) \ is used as escape character, and needs to be duplicated

Now clearly, after seeing just "[" fail, you could have tried it with "\\[" ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic