I have String some like this (Customer.Activity == "Car Loan") i am using below code to split the String using StringTokenizer in java
import java.util.ArrayList;
import java.util.StringTokenizer;
public class StringTokenizerClass {
public ArrayList<String> stringTokenizer(String str) {
StringTokenizer Tokenizer = new StringTokenizer(str);
ArrayList<String> tokenList = new ArrayList<String>();
while (Tokenizer.hasMoreTokens()) {
tokenList.add(Tokenizer.nextToken());
}
return (tokenList);
}
}
public class StringTokenizerMain {
public static void main(String[] args) {
String input = "(Customer.Activity == \"Car Loan\")";
StringTokenizerClass st = new StringTokenizerClass();
for (int i = 0; i < st.stringTokenizer(input).size(); i++) {
System.out.println(st.stringTokenizer(input).get(i));
}
}
}
And i am getting output like below
("Customer.Activity"
==
"Car
Loan")
But i am trying to achieve output like below
Can you suggest me way how can i achieve above output
If you are fine with a solution using Java regex API, given below the one which meets your requirement precisely:
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
List<String> tokenList = new ArrayList<String>();
String str = "(\"Customer.Activity\" == \"Car Loan\")";
Pattern pattern = Pattern.compile("[(\")]|\\w+.\\w+|==");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
tokenList.add(matcher.group());
}
for (String token : tokenList) {
System.out.println(token);
}
}
}
Output:
(
"
Customer.Activity
"
==
"
Car Loan
"
)