[英]Command “stringa.replaceAll(”*", stringb); causes an error
[英]get substring between a stringA and next stringB after stringA
我怎樣才能得到的線的部分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(","));
您可以使用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
以下代碼應該工作:
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);
感謝Tom,提供有關此API其他變體的提示。
我的答案
final String startPattern = "oid=";
final String endPattern = ",";
int startPatternIndex = response.indexOf(startPattern) + startPattern.length();
int endPatternIndex = response.indexOf(endPattern, startPatternIndex);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.