gpt4 book ai didi

java - 如何从字符串中获取特定字符串(子字符串)

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

我有像

这样的字符串
{Action}{RequestId}{Custom_21_addtion}{custom_22_substration}
{Imapact}{assest}{custom_23_multiplication}.

从此我只想要那些包含 "custom". 的子字符串

例如,我只想要上面的字符串

{Custom_21_addtion}{custom_22_substration}{custom_23_multiplication}.

我怎样才能得到这个?

最佳答案

您可以使用正则表达式,从 {custom} 进行查找。它看起来像这样:

Pattern pattern = Pattern.compile("\\{custom.*?\\}", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputString);

while (matcher.find()) {
System.out.print(matcher.group());
}

custom 后面的 .* 表示“custom”一词后面有 0 个或多个字符,问号将正则表达式限制为尽可能少的字符,这意味着它将在下一个 } 处中断它可以找到。

关于java - 如何从字符串中获取特定字符串(子字符串),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24056577/

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