【问题标题】:How to split string by comma and newline (\n) in Java? [duplicate]如何在 Java 中用逗号和换行符 (\n) 分割字符串? [复制]
【发布时间】:2020-07-16 15:25:44
【问题描述】:

所以我有这个字符串: "纽约,美国,1000\n" + "城市,世界,2000\n";

我需要先用换行符然后用逗号分割它,所以最后我得到一个字符串数组,如下所示:New York has index 0, USA index 1, 1000 index 2, World index 4 等等上..我尝试使用 String[] newline = string.split("\n") 然后声明一个名为 result 的新字符串数组(随机给它 1000 个字符)并像这样创建一个 for 循环:

String[] result = new String[1000];
for (String s : newline){
  result=s.split(",");
}

但它不能正常工作。请帮忙?

【问题讨论】:

  • 试过了,不行:/
  • 分享您尝试拆分的示例输入
  • @Kopite1905 好吧,我试过了,it works

标签: java string split


【解决方案1】:
public class Test {
    public static void main(String[] args) {
        String string = "New York,USA,1000\n" + "City,World,2000\n";
        String[] newline = string.split("\n");
        String[] result = new String[1000];
        //first loop
        result = newline[0].split(",");
        System.out.println(Arrays.toString(result));
        //second loop
        result = newline[1].split(",");
        System.out.println(Arrays.toString(result));
    }
}
/*
output:
[New York, USA, 1000]
[City, World, 2000]
 */

结果将被覆盖而不是追加。

public class Test {
    public static void main(String[] args) {
        String string = "New York,USA,1000\n" + "City,World,2000\n";
        String[] newline = string.split("\n");
        String[] result = new String[1000];
        int index = 0;
        for (String s : newline) {
            for (String t : s.split(",")) {
                result[index++] = t;
            }
        }
        for (int i = 0; i < index; i++) {
            System.out.print(result[i] + " ");
        }
    }
}
/*
output:
New York USA 1000 City World 2000 
 */

【讨论】:

    【解决方案2】:

    不同的操作系统有不同的新线

    请查看https://en.wikipedia.org/wiki/Newline

    • UNIX 或 Mac \r
    • Windows \r\n

      String[] lines = text.split("\\r?\\n");
      for (String line : lines) {
          System.out.println("line  : " + line);
          String[] words = line.split(",");
          System.out.println("words : " + words);
      }
      

    【讨论】:

    • OP 专门在问题标题和输入字符串中都使用了“\n”。
    • 但您没有指定您正在使用的操作系统
    【解决方案3】:

    如果您使用的是 java 8 或更高版本,请尝试以下操作:

    String str =  "New York,USA,1000\n" + "City,World,2000\n";
    String[] result = Pattern.compile("\n")
                             .splitAsStream(str)
                             .map(s -> s.split(","))
                             .flatMap(Arrays::stream)
                             .toArray(String[]::new);
    

    【讨论】:

      【解决方案4】:

      如果我没听错的话:

      import re
      
      stringobject = "New York,USA,1000\n" + "City,World,2000\n"
      
      emptylist = []
      for str_obj in stringobject.splitlines():
          o = str_obj.split(sep=",")
          emptylist.append(o)
      
      print(emptylist)
      
      [['New York', 'USA', '1000'], ['City', 'World', '2000']]
      

      这是一个列表,可以访问:

      print(emptylist[0][0])
      'New York'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-10
        • 2023-04-04
        • 1970-01-01
        • 2016-03-21
        相关资源
        最近更新 更多