简体   繁体   中英

Using delimiter in nextToken() with multiple delimiter in Java

I'm trying to work with a String that is structured like this:

3882027944,Yes,"A specific website or app (please provide the answer)",
"Weather Underground, also local tv news half the time.",
"Somewhat likely","30 - 44", Male, "$10,000 to $24,999",
"West North Central"

(Linebreaks for clarity, not in the original text)

I've been trying to do this using the nextToken() function in Java. I'm trying to split the string by each part between the commas. However, if a String contains a comma (eg Weather Underground, also local... ), it should not split there.

So my question is: Is there a way to split on a combination of tokens, so that I can split on "," instead of every seperate , ?

You might want to use split instead of a tokenizer

From http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html

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.

public static void main(String[] args) {
        String delims = "\",\"";
        String splitString = "one\",\"two,\",\"three\",\"four\",\",five";

        String[] tokens = splitString.split(delims);
        int tokenCount = tokens.length;
        for (int j = 0; j < tokenCount; j++) {
            System.out.println("Split Output: "+ tokens[j]);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM