0
String s = "author= {insert text here},";

Trying to get the inside of the string, ive looked around but couldn't find a resolution with just split or tokenizer...

so far im doing this

arraySplitBracket = s.trim().split("\\{", 0);

which gives me insert text here}, at array[1] but id like a way to not have } attached

also tried

StringTokenizer st = new StringTokenizer(s, "\\{,\\},");

But it gave me author= as output.

MasterYoshi
  • 57
  • 1
  • 7
  • can you clearly mention your input format?Does `s` and `author` refer to same variable ? `author= {insert text here},` in this you have a comma so it is not clear what is the exact format of string you are assigning to `author`. – Abhinav Oct 30 '18 at 06:04
  • Possible duplicate of [How to split a string in Java](https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – Rupesh S. Oct 30 '18 at 06:06
  • edited the s issue – MasterYoshi Oct 30 '18 at 06:10

4 Answers4

4
public static void main(String[] args) {
        String input="{a c df sdf TDUS^&%^7 }";     
        String regEx="(.*[{]{1})(.*)([}]{1})";
        Matcher matcher = Pattern.compile(regEx).matcher(input);            

        if(matcher.matches()) {         
            System.out.println(matcher.group(2));
        }
}
0

You can use \\{([^}]*)\\} Regex to get string between curly braces.

Code Snap :

String str = "{insert text here}";
        Pattern p = Pattern.compile("\\{([^}]*)\\}");
        Matcher m = p.matcher(str);
        while (m.find()) {
          System.out.println(m.group(1));
        }

Output :

insert text here
Snehal Patel
  • 1,282
  • 2
  • 11
  • 25
0
String s = "auther ={some text here},"; 
s =  s.substring(s.indexOf("{") + 1); //some text here},
s = s.substring(0, s.indexOf("}"));//some text here
System.out.println(s);
Rupesh S.
  • 41
  • 7
-1

How about taking a substring by excluding the character at arraySplitBracket.length()-1

Something like

arraySplitBracket[1] = arraySplitBracket[1].substring(0,arraySplitBracket.length()-1);

Or use String Class's replaceAll function to replace } ?

jkasper
  • 236
  • 4
  • 19