如何在Java中按逗号和换行符(\n)拆分字符串?

huangapple go评论113阅读模式
英文:

How to split string by comma and newline (\n) in Java?

问题

  1. String string = "New York,USA,1000\n" +
  2. "City,World,2000\n";
  3. String[] newline = string.split("\n");
  4. String[] result = new String[1000]; // It seems like you intended to initialize an array, not a string
  5. for (int i = 0; i < newline.length; i++) {
  6. result = newline[i].split(",");
  7. // Now you can access the elements of 'result' array for further processing
  8. }
英文:

So I have this string :
"New York,USA,1000\n" +
"City,World,2000\n";

I need to split it first by newline and then by comma, so at the end I get an array of strings that is like this: New York has index 0, USA index 1, 1000 index 2, World index 4 and so on..I tried using
String[] newline = string.split(&quot;\n&quot;) and then declaring a new array of strings called result ( giving it 1000 characters randomly ) and making a for loop like this:

  1. String[] result = new String[1000];
  2. for (String s : newline){
  3. result=s.split(&quot;,&quot;);
  4. }

But it doesn't work properly. Any help, please?

答案1

得分: 1

不同的操作系统有不同的换行符:

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

  • UNIX或Mac:\r

  • Windows:\r\n

    1. String[] lines = text.split(&quot;\\r?\\n&quot;);
    2. for (String line : lines) {
    3. System.out.println(&quot;line : &quot; + line);
    4. String[] words = line.split(&quot;,&quot;);
    5. System.out.println(&quot;words : &quot; + words);
    6. }
英文:

Different operating system has a different new line

Please check https://en.wikipedia.org/wiki/Newline

  • UNIX or Mac \r

  • Windows \r\n

    1. String[] lines = text.split(&quot;\\r?\\n&quot;);
    2. for (String line : lines) {
    3. System.out.println(&quot;line : &quot; + line);
    4. String[] words = line.split(&quot;,&quot;);
    5. System.out.println(&quot;words : &quot; + words);
    6. }

答案2

得分: 1

  1. 如果您正在使用Java 8或更高版本请尝试类似以下的代码
  2. String str = "New York,USA,1000\n" + "City,World,2000\n";
  3. String[] result = Pattern.compile("\n")
  4. .splitAsStream(str)
  5. .map(s -> s.split(","))
  6. .flatMap(Arrays::stream)
  7. .toArray(String[]::new);
英文:

If you are using java 8 or higher try try something like:

  1. String str = &quot;New York,USA,1000\n&quot; + &quot;City,World,2000\n&quot;;
  2. String[] result = Pattern.compile(&quot;\n&quot;)
  3. .splitAsStream(str)
  4. .map(s -&gt; s.split(&quot;,&quot;))
  5. .flatMap(Arrays::stream)
  6. .toArray(String[]::new);

答案3

得分: 1

  1. public class Test {
  2. public static void main(String[] args) {
  3. String string = "New York,USA,1000\n" + "City,World,2000\n";
  4. String[] newline = string.split("\n");
  5. String[] result = new String[1000];
  6. int index = 0;
  7. for (String s : newline) {
  8. for (String t : s.split(",")) {
  9. result[index++] = t;
  10. }
  11. }
  12. for (int i = 0; i < index; i++) {
  13. System.out.print(result[i] + " ");
  14. }
  15. }
  16. }
  17. /*
  18. output:
  19. New York USA 1000 City World 2000
  20. */
英文:
  1. public class Test {
  2. public static void main(String[] args) {
  3. String string = &quot;New York,USA,1000\n&quot; + &quot;City,World,2000\n&quot;;
  4. String[] newline = string.split(&quot;\n&quot;);
  5. String[] result = new String[1000];
  6. //first loop
  7. result = newline[0].split(&quot;,&quot;);
  8. System.out.println(Arrays.toString(result));
  9. //second loop
  10. result = newline[1].split(&quot;,&quot;);
  11. System.out.println(Arrays.toString(result));
  12. }
  13. }
  14. /*
  15. output:
  16. [New York, USA, 1000]
  17. [City, World, 2000]
  18. */

result would be overrided instead of append.

  1. public class Test {
  2. public static void main(String[] args) {
  3. String string = &quot;New York,USA,1000\n&quot; + &quot;City,World,2000\n&quot;;
  4. String[] newline = string.split(&quot;\n&quot;);
  5. String[] result = new String[1000];
  6. int index = 0;
  7. for (String s : newline) {
  8. for (String t : s.split(&quot;,&quot;)) {
  9. result[index++] = t;
  10. }
  11. }
  12. for (int i = 0; i &lt; index; i++) {
  13. System.out.print(result[i] + &quot; &quot;);
  14. }
  15. }
  16. }
  17. /*
  18. output:
  19. New York USA 1000 City World 2000
  20. */

答案4

得分: 0

  1. 如果我理解正确
  2. import re
  3. stringobject = "New York,USA,1000\n" + "City,World,2000\n"
  4. emptylist = []
  5. for str_obj in stringobject.splitlines():
  6. o = str_obj.split(sep=",")
  7. emptylist.append(o)
  8. print(emptylist)
  9. [['New York', 'USA', '1000'], ['City', 'World', '2000']]
  10. 可以访问的嵌套列表
  11. print(emptylist[0][0])
  12. 'New York'
英文:

If I get you correctly:

  1. import re
  2. stringobject = &quot;New York,USA,1000\n&quot; + &quot;City,World,2000\n&quot;
  3. emptylist = []
  4. for str_obj in stringobject.splitlines():
  5. o = str_obj.split(sep=&quot;,&quot;)
  6. emptylist.append(o)
  7. print(emptylist)
  8. [[&#39;New York&#39;, &#39;USA&#39;, &#39;1000&#39;], [&#39;City&#39;, &#39;World&#39;, &#39;2000&#39;]]

Which is a list of list and can be accessed:

  1. print(emptylist[0][0])
  2. &#39;New York&#39;

huangapple
  • 本文由 发表于 2020年4月4日 21:55:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/61029164.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定