Parse multiple doubles from a String(从字符串中解析多个双精度数)
问题描述
我想知道如何从一个字符串中解析几个双数,但是字符串可以混合,例如:String s = "text 3.454 sometext5.567568more_text"
.
I would like to know how to parse several double numbers from a string, but string can be mixed, for instance: String s = "text 3.454 sometext5.567568more_text"
.
标准方法(Double.parseDouble
)不合适.我尝试使用 isDigit
方法解析它,但是如何解析其他字符和 .
?
The standard method (Double.parseDouble
) is unsuitable. I've tried to parse it using the isDigit
method, but how to parse other characters and .
?
谢谢.
推荐答案
在使用此代码或其他帖子中的合适正则表达式解析双打后,迭代以将匹配的双打添加到列表中.在这里,您可以在代码中的其他任何地方使用 myDoubles
.
After parsing your doubles with the suitable regular expressions like in this code or in other posts, iterate to add the matching ones to a list. Here you have myDoubles
ready to use anywhere else in your code.
public static void main ( String args[] )
{
String input = "text 3.454 sometext5.567568more_text";
ArrayList < Double > myDoubles = new ArrayList < Double >();
Matcher matcher = Pattern.compile( "[-+]?\d*\.?\d+([eE][-+]?\d+)?" ).matcher( input );
while ( matcher.find() )
{
double element = Double.parseDouble( matcher.group() );
myDoubles.add( element );
}
for ( double element: myDoubles )
System.out.println( element );
}
这篇关于从字符串中解析多个双精度数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从字符串中解析多个双精度数


基础教程推荐
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 降序排序:Java Map 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01