How to using String.split in this case?

Posted by hoang nguyen on Stack Overflow See other posts from Stack Overflow or by hoang nguyen
Published on 2012-11-01T03:30:22Z Indexed on 2012/11/01 5:01 UTC
Read the original article Hit count: 163

Filed under:
|

I want to write a fuction like that:

- Input: "1" -> return : "1"
- Input: "12" -> return : ["1","2"]

If I use the function split(): String.valueOf("12").split("") -> ["","1","2"] But, I only want to get the result: ["1","2"].

What the best way to do this?

Infact, I can do that:

private List<String> decomposeQuantity(final int quantity) {
    LinkedList<String> list = new LinkedList<String>();
    int parsedQuantity = quantity;
    while (parsedQuantity > 0) {
        list.push(String.valueOf(parsedQuantity % 10));
        parsedQuantity = parsedQuantity / 10;
    }
    return list;
}

But, I want to use split() for having an affective code

© Stack Overflow or respective owner

Related posts about java

Related posts about regex