如何获取oid=
与此字符串之后的,
之间的行部分?
datatype=text, merged=true, title=Service, collapsed=true, filter={explicit=false, multiSelection=true, all=true}}, isCascading=false, disabled=false, instanceid=2D49C-C03A-21}], isPublic=null, oid=58f550fe3b143a902a0005b3, options={manual=true},
我尝试过执行以下操作,然而,它找到第一次出现,
,因此查找索引oid
和,
索引之间的数据发生在它之前,因此出错。
final String oid = response.substring(response.indexOf("oid=") + "oid=".length(), response.indexOf(","));
答案 0 :(得分:0)
您可以使用Pattern
和Matcher
来实现此目的:
String text = "datatype=text, merged=true, title=Service, collapsed=true, filter={explicit=false, multiSelection=true, all=true}}, isCascading=false, disabled=false, instanceid=2D49C-C03A-21}], isPublic=null, oid=58f550fe3b143a902a0005b3, options={manual=true},";
Pattern pattern = Pattern.compile("oid=([^,]+)");
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
System.out.println(matcher.group(1));
}
>> 58f550fe3b143a902a0005b3
答案 1 :(得分:0)
以下代码应该有效:
String oid = new String("datatype=text, merged=true, title=Service, collapsed=true, filter={explicit=false, multiSelection=true, all=true}}, isCascading=false, disabled=false, instanceid=2D49C-C03A-21}], isPublic=null, oid=58f550fe3b143a902a0005b3, options={manual=true},");
int startIndex = oid.indexOf("oid=") + "oid=".length();
oid = oid.substring(startIndex);
int endIndex = oid.indexOf(",");
oid = oid.substring(0, endIndex);
System.out.println(oid);
答案 2 :(得分:-1)
感谢Tom,关于此API的另一个变体的提示。
我的回答
final String startPattern = "oid=";
final String endPattern = ",";
int startPatternIndex = response.indexOf(startPattern) + startPattern.length();
int endPatternIndex = response.indexOf(endPattern, startPatternIndex);