用逗号分割后如何在连字符上分割?

我有以下输入字符串: Panama-Eduardo, Colombia-Elena

我用逗号分割字符串

String[] arr = input.split(",");

这导致

arr[0] = "Panama-Eduardo"
arr[1] = "Colombia-Elena"

我的问题是:以后如何在连字符处拆分这些字符串,以便获得以下结果:

第一组:

  • Panama

  • Eduardo

第2组:

  • Colombia

  • Elena


胡说叔叔
浏览 156回答 3
3回答

大话西游666

您只需再打split一次即可。例如:List<String[]>&nbsp;result&nbsp;=&nbsp;Arrays.stream(input.split(",")) &nbsp;&nbsp;&nbsp;&nbsp;.map(s&nbsp;->&nbsp;s.split("-")) &nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.toList());

慕工程0101907

请运行以下代码:public class SplitExplained {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; String input = "Panama-Eduardo, Colombia-Elena";&nbsp; &nbsp; &nbsp; &nbsp; String[] names = input.split(",");&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < names.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] strings = names[i].split("-");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Group " + (i + 1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(" ." + strings[0].trim());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(" ." + strings[1].trim());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

噜噜哒

您可以遍历数组以进一步拆分。List<String> list = new ArrayList<>();for (String s : arr) {&nbsp; &nbsp; list.add(s.split("-"));}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java