我有以下字符串,只有当字符串包含单词RUN
&
之后将其剪切到字符RUN
portal.sidiv.registrocivil.cl/docstatus?RUN11111111-1&typeCEDULA&serial107172548&mrz29272904762037262937
答案 0 :(得分:2)
public static String cutString(String target, String pattern) {
if (target.contains(pattern)) {
return target.split(pattern)[1].substring(0, target.split(pattern)[1].indexOf('&'));
}
return target;
}
并测试:
public static void main (String[] args){
System.out.println(cutString("portal.sidiv.registrocivil.cl/docstatus?RUN11111111-1&typeCEDULA&serial107172548&mrz29272904762037262937", "RUN"));
}
返回:11111111-1
答案 1 :(得分:1)
这是一个快速的代码片段:
String str = "portal.sidiv.registrocivil.cl/docstatus?RUN11111111-1&typeCEDULA&serial107172548&mrz29272904762037262937";
String result = str.contains("RUN") ? str.substring(str.indexOf("RUN") + 3,str.indexOf("&")) : null;
if(result != null) {
System.out.println(result);
}
输出:
11111111-1