0


I'm looking for a way to realise the following problem.

Starting from a String a I try to return a String[] equals

new String[]{Q},{ec.Qh},{ec.Q2}

Does anyone have any sugestions?

So far:

    String a="Q={{ec.Qh}{ec.Q2}}";

    int index_first = a.indexOf("="); 
    String name= a.substring(0,index_first); 
    String temp=a.substring(index_first+1, a.length());
    temp=temp.replaceAll("\\{\\{","");
    temp=temp.replaceAll("\\}\\{","\\,");
    temp=temp.replaceAll("\\}\\}","");
    String[] synonyms=temp.split(",");

    //for(int i =0;i<synonyms.length;i++){System.out.println(synonyms[i]);}

    String[] result=new String[]{name,synonyms} // does not work! 
1
  • 1
    Your question is not clear.. what exactly are you trying to achieve ? Commented Nov 5, 2012 at 9:07

2 Answers 2

3

You could use a StringTokenizer thus:

new StringTokenizer(string, "{}")

Note that the StringTokenizer splits on the nominated characters (} and {) and doesn't return these. You should iterate through the tokenizer results using nextToken() and add the results to a List<String>.

It's useful here because of its simplicity but the documentation notes that StringTokenizer is a legacy class, so you should use with discretion.

Sign up to request clarification or add additional context in comments.

Comments

2

You can use

String s = "Q={{ec.Qh}{ec.Q2}}";
String[] parts = s.split("(=\\{\\{|}\\{|}})");
System.out.println(Arrays.toString(parts));

prints

[Q, ec.Qh, ec.Q2]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.