String text = "/'Team1 = 6', while /'Team2 = 4', and /'Team3 = 2'";String[] body = text.split("/|,");String b1 = body[1];String b2 = body[2];String b3 = body[3];预期结果:b1 = 'Team1 = 6'b2 = 'Team2 = 4'b3 = 'Team3 = 2'
4 回答
浮云间
TA贡献1829条经验 获得超4个赞
使用正则表达式。像这样的东西:
String text = "/'Team1 = 6', while /'Team2 = 4', and /'Team3 = 2'";
Matcher m = Pattern.compile("(\\w+\\s=\\s\\d+)").matcher(text);
// \w+ matches the team name (eg: Team1). \s=\s matches " = " and \d+ matches the score.
while (m.find()){
System.out.print(m.group(1)+"\n");
}
这打印:
团队 1 = 6
团队 2 = 4
团队 3 = 2
呼啦一阵风
TA贡献1802条经验 获得超6个赞
添加回答
举报
0/150
提交
取消