1

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

enter image description here

Can you suggest me way how can i achieve above output

2
  • Rahul - Is the solution limited to by using StringTokenizer only or you are fine with Java regex API as well? Commented Apr 23, 2020 at 8:13
  • i fine with regex too Commented Apr 23, 2020 at 13:47

3 Answers 3

1

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
"
)
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Aravind thanks for the reply i have one issue if the string is bigger than Car Loan let say "BETWEEN 2400 AND 600" the string is getting splitting into two tokens can you please tell how to get it in single tokens.
@Rahul - I have a solution to deal with this issue but that will fail to capture " on a separate line i.e. either " can be ignored or captured like "BETWEEN 2400 AND 600". Will that meet your requirement?
@ Arvind Kumar Avinash but i need to capture " in separate line
1

I think you need:

StringTokenizer st = new StringTokenizer(input, "\"");
while (st.hasMoreTokens()) {
  System.out.println(st.nextToken());
}

Comments

1

You need to add valid deliminator,

StringTokenizer Tokenizer = new StringTokenizer(str,"\\\"", true);

Pass, returnDelims=true as you need " in result.

With updated question:

You can use "=(\"" but for ==, you can not use any delim,

(
Customer.Activity 
=
=

"
Car Loan
"
)

Note from java docs,

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

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.