-9

I need to split a text (String), the text is just like this:

[title],[description blablablablablabla]

Is there a way to do this? I need to store in a String[] the 2 texts between brackets, separated by a ",".

David Conrad
  • 15,432
  • 2
  • 42
  • 54
erickles
  • 157
  • 2
  • 16
  • 1
    String split function. http://docs.oracle.com/javase/6/docs/api/java/lang/String.html – jrowe08 Mar 24 '14 at 20:04
  • [`string.split(",")`](http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split(java.lang.String)) – Whymarrh Mar 24 '14 at 20:04
  • 3
    @jrowe08 That documentation is very out of date. Use http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String- instead. – The Guy with The Hat Mar 24 '14 at 20:04
  • 1
    @TheGuywithTheHat Considering that it was released a week ago, chances are they're not using Java 8. – Whymarrh Mar 24 '14 at 20:08
  • @TheGuywithTheHat ...It's the same exact documentation for the method, just with a different font. – asteri Mar 24 '14 at 20:08
  • i wonder why you posted this question here, as you know the answer clearly "Java: split text" . Had you googled, you would have got it directly from the docs. – spiderman Mar 24 '14 at 20:17
  • 1
    Can the text in the brackets contain a `","`? Can the text in brackets contain brackets, escaped somehow? E.g., `"[one \[two\] three]"`? – David Conrad Mar 24 '14 at 20:18
  • Yes, the text can have a ",". That´s my problem...i was using the String.split() as many of you have answered me. – erickles Mar 25 '14 at 18:25

4 Answers4

9

You can use the split(String str) method of class String. Example:

String resultArray[] = stringToSplit.split(",");
Endrik
  • 2,238
  • 3
  • 19
  • 33
  • 1
    How will this work if the `[title],[description]` is `"[Hans Brinker, or The Silver Skates],[A colorful portrait of early nineteenth-century Dutch life, as well as a tale of youthful honor.]"`? – David Conrad Mar 24 '14 at 20:23
  • That is exactly my doubt. Inside a bracket could have a ",". – erickles Mar 25 '14 at 18:24
3

You can use Pattern and Matcher:

String input = "[title],[description blablablablablabla]";

Pattern pattern = Pattern.compile("\\[(.*?)\\]");
Matcher matcher = pattern.matcher(input);
ArrayList<String> stringList = new ArrayList<String>();

while(matcher.find()) {
    stringList.add(matcher.group(1));
}

//If you just need the results to be stored in an array of Strings anyway.
String[] stringArray = stringList.toArray(new String[stringList.size()]); 
Hari
  • 1,509
  • 15
  • 34
1

Yes, it is quite easy, try this :

    String text = "[title],[description blablablablablabla]";        
    String[] splitted = text.replaceAll("\\[", "").replaceAll("\\]", "").split(",");
libik
  • 22,239
  • 9
  • 44
  • 87
  • 2
    just fyi, when the replacement term doesn't need to be regex (as here), you can simply code `text.replace("[", "").replace("]", "").split(",");`. Or you could use a regex, but combine the two replacements into one call: `text.replaceAll("\\[|\\]", "").split(",");`. Less code == good :) – Bohemian Mar 24 '14 at 23:43
1

To split string you can use this method:

public String[] split(String regex, int limit)

Or

public String[] split(String regex)

For more details visit http://www.tutorialspoint.com/java/java_string_split.htm

Example:

String a = "Hi guys, what's up?"
String[] b = a.split(",");
Braiam
  • 1
  • 11
  • 47
  • 78
Caco85
  • 23
  • 6