-4

我有以下字符串“如果这是好的,如果那是坏的”。要求是从主字符串中提取字符串“that”。

使用

substringBetween(mainString, "If", "is") returns the string "this".

在这种情况下,您能否帮助提取所需的字符串。如果使用函数 substringBetween() 无法做到这一点,是否有任何替代字符串函数可以实现这一点?

4

2 回答 2

0

You can use regex and Pattern matching to extract it, e.g.:

String s = "If this is good and if that is bad";
Pattern pattern = Pattern.compile("if(.*?)is");
Matcher m = pattern.matcher(s);
if(m.find()){
    System.out.println(m.group(1).trim());
}
于 2017-06-27T07:51:10.010 回答
0

你的意思 StringUtils.substringBetween(foo, "if", "is")不是 StringUtils.substringBetween(foo, "If", "is")因为 substringBetween 方法是区分大小写的操作 在此处输入图像描述

在“If”和“is”之间搜索与在“if”和“is”之间搜索的结果不同

String foo = "If this is good and if that is bad";
String bar = StringUtils.substringBetween(foo, "if", "is");
// String bar = StringUtils.substringBetween(foo, "If", "is");
System.out.println(bar);
于 2017-06-27T07:44:34.787 回答