从Java中的字符串中提取浮点值 [英] Extracting Float values from a string in Java
本文介绍了从Java中的字符串中提取浮点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样一个字符串湿度:14.00%温度:28.00 C
。
从字符串中提取两个浮点值,我该怎么做?
首先,看看 @Bathsheba 的评论。我有一个直截了当的方式,但这不是聪明或普遍的。 (湿度:|温度:| C]).split(字符串数组 %);
您将收到字符串[] {14.00,28.00}
然后你可以通过使用 Float.parse()$ c $将这些数组的值转换为
Float
c>或 Float.valueOf()
方法。
Arrays.stream (阵列).MAP(浮法::的valueOf).toArray(浮法[] ::新);
I have a string like this "Humidity: 14.00% Temperature:28.00 C"
.
I want to extract the two float values from the string, how can I do so?
解决方案
First, take a look at @Bathsheba`s comment.
I have a straightforward way, but that is not clever or universal.
String[] array = string.replaceAll("[Humidity:|Temperature:|C]", "").split("%");
You will receive String[]{"14.00", "28.00"}
and then you may convert these values of the array into Float
by using Float.parse()
or Float.valueOf()
methods.
Arrays.stream(array).map(Float::valueOf).toArray(Float[]::new);
这篇关于从Java中的字符串中提取浮点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文