-1

What should I do to for separate (test(1,2) , test(3,4)) as test(1,2) and test(3,4). What can I use as delimiter value instead of x here? Thanks

    StringTokenizer tokenizer = new StringTokenizer(str, "x");
TT.
  • 15,774
  • 6
  • 47
  • 88
Mrkrky
  • 73
  • 5
  • don't use code like this. StringTokenizer has been deprecated for several versions of Java. Use the new options/functionalities of Java – Stultuske Feb 17 '20 at 06:45
  • User `String.split()` instead. You can provide a regexp there to do the job. Also, is it a simplified example? Can you have more than two arguments for a `test`? Nested tests? If that is, you probably want to use a parser instead. – Pavel Smirnov Feb 17 '20 at 06:48
  • @Pavel Smirnov i will wont have more than 2 arguments. Will be test(4,5) , test2(6,7) , test3( 78,63) test4(..... – Mrkrky Feb 17 '20 at 06:57
  • Please have a look at: https://www.baeldung.com/java-split-string If anything is still unclear then, feel free to ask. – Seb Feb 17 '20 at 07:29

2 Answers2

0

Try this :-

    String s = "(test(1,2) , test(3,4))";
    s=s.substring(1).substring(0,s.length()-2);
    String[] arr = s.split(" ,");
    System.out.println(arr[0].trim());
    System.out.println(arr[1].trim());
Gaurav Dhiman
  • 953
  • 6
  • 11
0

Use String.split() instead:

String s = "test(1,2), test2(4,5), test3(2,3)";
String[] arr = s.split("(?<=\\w*[(]\\d,\\d[)]),");
System.out.println(Arrays.toString(arr));

Output:

[test(1,2),  test2(4,5),  test3(2,3)]
Pavel Smirnov
  • 4,611
  • 3
  • 18
  • 28