Tokenization of input string without a delimiter

 ̄綄美尐妖づ 提交于 2020-05-15 21:22:20

问题


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


回答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
"
)



回答2:


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.




回答3:


I think you need:

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


来源:https://stackoverflow.com/questions/61381621/tokenization-of-input-string-without-a-delimiter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!