gpt4 book ai didi

java - 我需要从 Java 中的字符串中提取数字

转载 作者:行者123 更新时间:2023-11-29 03:32:50 24 4
gpt4 key购买 nike

在我的程序中,我需要从字符串中提取数字,给定的字符串如下。

String numberOfHours = "12.0  8.0  7.0  7.0  10.0  8.0  0.0  2.0";

我需要将每个值提取到一个数组中。当我使用 String 类的 split 方法时,我得到一个空值,而且我没有得到数组中的所有数字。这是代码。

   String pieces[] = numberOfHours.split("  ");  

for(int i = 0 ; i < hoursPerDay.length ; i++){
System.out.println(pieces[i]);
}

提前致谢!

最佳答案

这个:

String numberOfHours = "12.0  8.0  7.0  7.0  10.0  8.0  0.0  2.0";
String pieces[] = numberOfHours.split("\\s+");
System.out.println(pieces.length);

打印:“8”。是您要找的吗?

关于java - 我需要从 Java 中的字符串中提取数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17224222/

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