How to using String.split in this case?
- by hoang nguyen
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