gpt4 book ai didi

java - java中如何过滤字符串

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

我在 Java 中有以下字符串。例如:

String abc = "nama=john; class=6; height=170; weight=70";

如何从字符串中提取height的值?

输出:height=170

这是我到目前为止编写的代码:

String abc = "nama=john; class=6; height=170; weight=70";
String[] tokens = abc.split("; ");
List<String> listString = new ArrayList<String>();
String mod = new String();

for (String s : tokens) {
mod = s;
System.out.println(s);
listString.add(mod);
}

System.out.println(listString.size());

但我没有得到高度值。相反,我以字符串形式获取高度值。

谢谢。

最佳答案

使用此代码片段:

String abc = "nama=john; class=6; height=170; weight=70";
for(String sa : abc.split(";")){
System.out.println(sa.trim());
}

您生成此输出:

nama=john
class=6
height=170
weight=70

如果要将特定字符串添加到列表中,请将 sa.trim() 放在 List.add 参数中。要查找 height - 字符串,您可以使用:if(sa.trim().startsWith("height")) 并且您拥有所需的字符串。

关于java - java中如何过滤字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32089839/

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