gpt4 book ai didi

java - 提取特定单词后由分隔符分隔的值

转载 作者:行者123 更新时间:2023-12-01 18:00:49 24 4
gpt4 key购买 nike

假设 a 有一个类似以下的主字符串:“最新价格是三星(100.59)、诺基亚(35.23)、苹果(199.34)”。有没有什么方法可以通过发送名称来从此字符串中提取值?

public Double getValue(字符串名称);

因此 getValue("Nokia") 将返回 35.23,getValue(Apple) 将返回 199.34。

最佳答案

您可以使用正则表达式来做到这一点

public Double getValue(String name){
Pattern p = Pattern.compile("(?<=" + name + "\\()\\d+\\.\\d+(?=\\))");
Matcher m = p.matcher("<your matcher string>");
m.find();
return Double.parseDouble(m.group());
}

关于java - 提取特定单词后由分隔符分隔的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41114275/

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