0

For Example : I have a string coming from some Api call like below : And I want to split based on [DART1].

String value = "University[DART1]BUCKKKKK";
String[] splitData = value.split("DART1");
String firstWord = splitData[0].substring(0, splitData[0].length() - 1);
String secondWord = splitData[1].substring(1);

I did a split and removed those special characters from the string, but I was doing lot of operation on string.

Is there a better way of doing this?

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
Pradeep
  • 1,192
  • 2
  • 12
  • 30

1 Answers1

0

Split based on "\\[DART1]"

String[] result = value.split("\\[DART1]");
  1. The \\ is required to escape the [ otherwise the compiler would think you want to split on anything that's either D, A, R, T or 1.

Then you can access the individual elements by indexing into the result array.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126