Rohit Pandey
Rohit Pandey

Reputation: 2681

Java StringTokenizer with empty tokens

I have a string that looks some thing like - 56,0,76,0,93,,,,,,,1230. I use a StringTokenizer to split this by ','. However, it seems that this is skipping from 93 straight to 1230. Is there a way to make it return six empty strings before it moves to 1230?

Upvotes: 8

Views: 9046

Answers (8)

Aftershock
Aftershock

Reputation: 5351

I have this answer.. which is similar to the previous one..it has no bugs as far as I know.. based on using StringTokenizer with true arguments at the end.

// inputs:  text, delimiters
    Vector tokens;
    tokens = new Vector();
    boolean lastissep = false;
    tokenizer = new StringTokenizer(text, delimeters,true);
    while (tokenizer.hasMoreTokens()) {
        Object o = tokenizer.nextElement();
        if (((String)(o)).equals(delimeters))
        {
            tokens.addElement("");
            lastissep = true;
        }
        else
        {
            tokens.addElement(o);
            lastissep = false;
            if (tokenizer.hasMoreTokens()) 
            {
                tokenizer.nextElement();
                lastissep = true;
            }   
        }   
          
    }
    if (lastissep)
        tokens.addElement("");

Upvotes: 1

Anand
Anand

Reputation: 1871

Please find a working copy to overcome Empty Token

public class TestStringTokenStrict {

/**
 * Strict implementation of StringTokenizer
 * 
 * @param str
 * @param delim
 * @param strict
 *            true = include NULL Token
 * @return
 */
static StringTokenizer getStringTokenizerStrict(String str, String delim, boolean strict) {
    StringTokenizer st = new StringTokenizer(str, delim, strict);
    StringBuffer sb = new StringBuffer();

    while (st.hasMoreTokens()) {
        String s = st.nextToken();
        if (s.equals(delim)) {
            sb.append(" ").append(delim);
        } else {
            sb.append(s).append(delim);
            if (st.hasMoreTokens())
                st.nextToken();
        }
    }
    return (new StringTokenizer(sb.toString(), delim));
}

static void altStringTokenizer(StringTokenizer st) {
    while (st.hasMoreTokens()) {
        String type = st.nextToken();
        String one = st.nextToken();
        String two = st.nextToken();
        String three = st.nextToken();
        String four = st.nextToken();
        String five = st.nextToken();

        System.out.println(
                "[" + type + "] [" + one + "] [" + two + "] [" + three + "] [" + four + "] [" + five + "]");
    }
}

public static void main(String[] args) {
    String input = "Record|One||Three||Five";
    altStringTokenizer(getStringTokenizerStrict(input, "|", true));
}}

Output will be

[Record] [One] [ ] [Three] [ ] [Five]

Upvotes: 3

Balicanta
Balicanta

Reputation: 109

Use Google's Guava lib Splitter

String str = "56,0,76,0,93,,,,,,,1230";
Iterable<String> splitResult = Splitter.on(",").split(str);

and transform to a ArrayList

ArrayList<String> elementList = Lists.newArrayList(splitResult);

elementList.size(); // 12

Upvotes: 0

Braj
Braj

Reputation: 46841

Use String#split(regex,limit) along with limit as -1

sample code:

System.out.println(Arrays.toString(",,-56,0,76,0,93,,,,,,,1230,".split(",",-1)));

output:

[, , -56, 0, 76, 0, 93, , , , , , , 1230, ]

Upvotes: 4

Seyf
Seyf

Reputation: 889

StringTokenizer has a constructor which takes 3 arguments which the third is a boolean that controls if the delimiters are to be returned as tokens or not. You should set it to true.

new StringTokenizer(yourString, yourDelimiters, true);

Now the delimiters are returned too, and it is easy to tell if an empty string exists by checking if two consecutive tokens are of delimiters.

You can extend the tokenizer or write a wrapper class of your own for reuse purposes.

Upvotes: 3

coffeeaddict
coffeeaddict

Reputation: 868

String.split(",",-1); Without the -1 it will ignore trailing delimiter

Upvotes: 1

lxcky
lxcky

Reputation: 1668

Use String.split() method instead.

String str = "56,0,76,0,93,,,,,,,1230";
String[] stringArr = str.split(",");

This will return an array of String.

Upvotes: 7

Adam Siemion
Adam Siemion

Reputation: 16039

Use StringTokenizer(String str, String delim, boolean returnDelims) with returnDelims set to true

If the returnDelims flag is true, then the delimiter characters are also returned as tokens. Each delimiter is returned as a string of length one.

Upvotes: 1

Related Questions