您可以使用正则表达式。
这是完整的例子
package snippet;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) throws CoffeeDOMException, IOException {
String test = "Thanks, this is your value : 100 . And this is your account number : 219AD098";
String valueExpression = "\\svalue\\s:([^.]+)";
String accExpresion = "\\saccount\\snumber\\s:([^$]+)";
System.out.println("Value:" + runSubRegex(valueExpression, test));
System.out.println("Account:" + runSubRegex(accExpresion, test));
}
private static String runSubRegex(String regex, String tag) {
Pattern p = Pattern.compile(regex);
Matcher matcher = p.matcher(tag);
if (matcher.find()) {
return matcher.group(1);
}
return null;
}
}
输出
Value: 100
Account : 219AD098