8

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?

Rohit Pandey
  • 2,443
  • 7
  • 31
  • 54

8 Answers8

7

Use String.split() method instead.

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

This will return an array of String.

lxcky
  • 1,668
  • 2
  • 13
  • 26
3

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, ]
Braj
  • 46,415
  • 5
  • 60
  • 76
3

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.

Seyf
  • 889
  • 8
  • 16
3

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]
Anand
  • 1,845
  • 2
  • 20
  • 25
1

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.

Adam Siemion
  • 15,569
  • 7
  • 58
  • 92
1

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

coffeeaddict
  • 858
  • 5
  • 3
1

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("");
Aftershock
  • 5,205
  • 4
  • 51
  • 64
0

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
Balicanta
  • 109
  • 6