return : "1" - Input: "12" -> return : ["1","2"] 如果我使用函数 split():String.-6ren">
gpt4 book ai didi

java - 在这种情况下如何使用 String.split?

转载 作者:行者123 更新时间:2023-11-29 08:03:07 25 4
gpt4 key购买 nike

我想写一个这样的函数:

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

如果我使用函数 split():String.valueOf("12").split("") -> ["","1","2"]但是,我只想得到结果:["1","2"].

最好的方法是什么?

事实上,我可以做到:

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;
}

但是,我想使用 split() 来获得情感代码

最佳答案

String input = "123";
String regex = "\\d";
Matcher m = Pattern.compile(regex).matcher(input);

String[] output = new String[input.length()];
int index = 0;

while (m.find())
output[index++] = m.group();

for (String s : output)
System.out.println(s);

关于java - 在这种情况下如何使用 String.split?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13170551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com