gpt4 book ai didi

java - 如何拆分字符串并查找数字?

转载 作者:太空宇宙 更新时间:2023-11-04 08:25:01 26 4
gpt4 key购买 nike

String[] tokens = s.split(' ');
for(int i = 0; i < (tokens.length) - 1; ++i)
{
if(isDigit(tokens[i]))
{
// Two numbers cannot be together without an operator.
if(isDigit(tokens[i + 1]))
{
check = 1;
break;
}
if((tokens[i + 1] == '(') )
{
check = 1;
break;
}
}
}

s 是我要分割的字符串。分割后我想检查每个分割部分是否是一个数字。我无法使用 isDigit,因为它只能用于字符,而这里的分割部分是字符串。

注意:我正在为计算器编写程序。如果我使用 toCharArray() ,多于一位的数字将被分割。例如。 23将分别变成2和3。

最佳答案

int isDigit = false;
try
{
Integer.parseInt(tokens[i])
isDigit = true;
}
catch(NumberFormatException e)
{

}

关于java - 如何拆分字符串并查找数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8742768/

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