我想将String
分成两个子字符串,如下所示:
String value = "Verify Payment 2";
预计这两部分将是:
String one = Verify Payment
String two = 2
我使用分割功能(按空格分割),但它给出了3个字符串的结果:
String[] output = eachElement.getText().split("\\s");
答案 0 :(得分:2)
试试这个:
String s= "Verify Payment 2";
Pattern p =Pattern.compile("(.+)\\s(\\w+)");
Matcher m = p.matcher(s);
if (m.find()){
System.out.println("String 1 ="+m.group(1));
System.out.println("String 2 ="+m.group(2));
}