然后我想单独选择团队: 西汉姆联队 奥兰多海盗队 凯泽酋长 埃弗顿... 我决定使用split将它们添加到Array上,这样我就可以使用它们的索引号来选择它们。我得到了第一支球队和最后一支球队,但是我之间的球队得到了类似的结果: 的println(choose_team [3]); 曼联阿森纳,但我个人想要他们。请看代码!任何解决方案?
字符串答案="&#34 ;;
//以下字符串游戏是我要提取和存储的团队示例。
String games = "West Ham United - Orlando Pirates Kaizer Chiefs - Everton Liverpool - Manchester United Arsenal - Real Madrid";
// below I want to split and store as an array
String[] numbers = games.split("-");
System.out.println("splited String variable after loop:");
for(String ans : numbers){
System.out.println(ans);
answer += ans + "-"; }
answer = answer.substring(0,answer.length()-1);
String[] choose_team = answer.split("-");
System.out.println("the - sign is re-instated:" + "\n" + answer);
// I want to get the teams individually and a print statement is just showing what I want but I'm going to use the teams somewhere else in a code.
System.out.println("splited string with index 4: " + "\n" + choose_team[0]);
System.out.println("splited string with index 0: " + "\n" + choose_team[4]);
System.out.println("splited string with index 1: " + "\n" + choose_team[1]);
System.out.println("splited string with index 2: " + "\n" + choose_team[2]);
答案 0 :(得分:0)
如果我理解你的问题,我认为这可以提供帮助
final String games = "Team Alfa - Team Bravo Team Charlie - Team Delta Team Echo - Team Foxtrot Team Golf - Team Hotel";
final String[] numbers = games.split("(?=Team)");
final List teams = new ArrayList<String>();
System.out.println("splited String variable after loop:");
for (String ans : numbers) {
ans = ans.replaceAll("[-+^]*", "");
System.out.println(ans);
teams.add(ans);
}
for (int i = 0; i < teams.size(); i++) {
System.out.println("Team No. " + i + " " + teams.get(i));
}