在Java中如何使用System.lineseparator和逗号来拆分字符串?

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

How do I split a string in Java with System.lineseparator AND commas?

问题

我有一个整数文件,类似于以下内容:

9,4, 11, 2, 40, 17,11,20,9,20, 15,13, 19, 35,12,14, 13,1, 0,20,9, 1
40,29, 40, 25, 30, 12, 31, 27,39,7,37,15,31,1,26, 36,36, 35,30, 5, 24
25, 9, 20,31, 18, 11, 33,22, 39,40, 4, 23,34, 13, 16, 19, 22,3, 36, 2 
18,23,16,2, 28,0, 9,15, 5,22, 35, 40,2,14,18, 33, 22,16,12, 35, 37,38,2
0,24, 29,37, 13,3, 9,18,39, 8,24,9,1,39,22,2, 27, 38,5, 38, 6, 11,13,10 
31,25,22, 16,2, 1,25, 26,30, 10, 36, 18,33,31, 27, 30,25, 12, 5,5,2, 35
7,7,36,16, 33,12,34, 19, 29, 40,5,7

这些整数由逗号分隔,行由System.lineseparator()分隔。

使用readFile方法将TextFile转换为字符串。现在我想将该字符串转换为整数值的1D字符串数组。

但是,当我使用逗号(",")拆分字符串时,行末的值会丢失,因为它们不是由逗号而是由行分隔符分隔的。我该如何解决这个问题?

我尝试仅按逗号分隔,因为我认为拆分会自动读取行分隔符。然而,情况并非如此。而且 Java 不允许对字符串进行两次拆分。

英文:

I have a file of integers, similar to the following:

9,4, 11, 2, 40, 17,11,20,9,20, 15,13, 19, 35,12,14, 13,1, 0,20,9, 1
40,29, 40, 25, 30, 12, 31, 27,39,7,37,15,31,1,26, 36,36, 35,30, 5, 24
25, 9, 20,31, 18, 11, 33,22, 39,40, 4, 23,34, 13, 16, 19, 22,3, 36, 2 
18,23,16,2, 28,0, 9,15, 5,22, 35, 40,2,14,18, 33, 22,16,12, 35, 37,38,2
0,24, 29,37, 13,3, 9,18,39, 8,24,9,1,39,22,2, 27, 38,5, 38, 6, 11,13,10 
31,25,22, 16,2, 1,25, 26,30, 10, 36, 18,33,31, 27, 30,25, 12, 5,5,2, 35
7,7,36,16, 33,12,34, 19, 29, 40,5,7

The integers are separated by commas and the rows are separated by the System.lineseparator().

The TextFile is converted into a string by the use of the readFile method. Now I would like to convert that string into a 1D array of strings of the integer values.

However, when I split the string by commas (",") I loose the values at the end of a row as they are not separated by commas but by the lineseparator. How can I solve this problem?

I tried to separate it only by commas, as I thought that the lineseparator gets automatically read by the split. However, this is not the case. Also Java does not allow to split a string twice.

答案1

得分: 2

使用\R来匹配(任何)系统换行符:

String[] values = str.split("\\R|,");

查看live demo

注意:在字符类内部\R不起作用。


如果你想要将值作为数组或int

int[] ints = Arrays.stream(str.split("\\R|,"))
  .mapToInt(Integer::parseInt)
  .toArray();
英文:

Use \R to match (any) system line separator:

String[] values = str.split("\\R|,");

See live demo.

Note: \R doesn't work within a character class.


If you want the values as an array or int:

int[] ints = Arrays.stream(str.split("\\R|,"))
  .mapToInt(Integer::parseInt)
  .toArray();

答案2

得分: 1

import java.nio.file.*;
import java.util.*;

public class Nums {
    public static void main(String[] args) {
        try (Scanner s = new Scanner(Files.newBufferedReader(Path.of("numbers.txt")))) {
            s.useDelimiter("[,\r\n]");
            String[] numbers = s.tokens()
                .map(String::trim)
                .toArray(String[]::new);
            System.out.println(Arrays.toString(numbers));
        }
        catch(Throwable t) {
            t.printStackTrace();
        }
    }
}
英文:

A variant on @Bohemian, using Scanner from file:

import java.nio.file.*;
import java.util.*;

public class Nums {
    public static void main(String[] args) {
        try (Scanner s = new Scanner(Files.newBufferedReader(Path.of("numbers.txt")))) {
                s.useDelimiter("[,\r\n]");
                String[] numbers = s.tokens().
                map(String::trim).
                toArray(String[]::new);
            System.out.println(Arrays.toString(numbers));
        }
        catch(Throwable t) {
            t.printStackTrace();
        }
    }
}

答案3

得分: 0

您可以使用_String#replaceAll_方法首先移除换行符。

以下模式足以移除_换行符_。

(?:\r\n?|\n)+

这是一个示例。

String string = "1,2,3" + System.lineSeparator() + "4,5,6";
String[] strings = string.replaceAll("(?:\\r\\n?|\\n)+", ",").split(",");
System.out.println(Arrays.toString(strings));

输出

[1, 2, 3, 4, 5, 6]
英文:

You can use the String#replaceAll method to remove the line separators, first.

The following pattern should be sufficient to remove the new-line delimiters.

(?:\r\n?|\n)+

Here is an example.

String string = "1,2,3" + System.lineSeparator() + "4,5,6";
String[] strings = string.replaceAll("(?:\\r\\n?|\\n)+", ",").split(",");
System.out.println(Arrays.toString(strings));

Output

[1, 2, 3, 4, 5, 6]

答案4

得分: 0

假设您希望将它们用作 ints,您可以在空格、逗号和换行符上进行拆分。

String s = """
9,4, 11, 2, 40, 17,11,20,9,20, 15,13, 19, 35,12,14, 13,1, 0,20,9, 1
40,29, 40, 25, 30, 12, 31, 27,39,7,37,15,31,1,26, 36,36, 35,30, 5, 24
25, 9, 20,31, 18, 11, 33,22, 39,40, 4, 23,34, 13, 16, 19, 22,3, 36, 2 
18,23,16,2, 28,0, 9,15, 5,22, 35, 40,2,14,18, 33, 22,16,12, 35, 37,38,2
0,24, 29,37, 13,3, 9,18,39, 8,24,9,1,39,22,2, 27, 38,5, 38, 6, 11,13,10 
31,25,22, 16,2, 1,25, 26,30, 10, 36, 18,33,31, 27, 30,25, 12, 5,5,2, 35
7,7,36,16, 33,12,34, 19, 29, 40,5,7
""";

String[] stringArray = s.split("\\R|[\\s,]+");       
int count = 1;
for (String str : stringArray) {
    System.out.print(str + " " );
    if (count % 20 == 0) {
        System.out.println();
    }
    count++;
}

为避免水平滚动,每行最多打印20个。

9 4 11 2 40 17 11 20 9 20 15 13 19 35 12 14 13 1 0 20 
9 1 40 29 40 25 30 12 31 27 39 7 37 15 31 1 26 36 36 35 
30 5 24 25 9 20 31 18 11 33 22 39 40 4 23 34 13 16 19 22 
3 36 2 18 23 16 2 28 0 9 15 5 22 35 40 2 14 18 33 22 
16 12 35 37 38 2 0 24 29 37 13 3 9 18 39 8 24 9 1 39 
22 2 27 38 5 38 6 11 13 10 31 25 22 16 2 1 25 26 30 10 
36 18 33 31 27 30 25 12 5 5 2 35 7 7 36 16 33 12 34 19 
29 40 5 7 

如果您想将它们转换为一个 int 数组,请按如下方式操作。请注意,您需要消除空格以避免 NumberFormatException

int[] intArray = Arrays.stream(s.split("\\R|[\\s,]+"))
                .mapToInt(Integer::parseInt).toArray();
英文:

Assuming you want to use them as ints, you can split on white space while also splitting on a comma and the line separator.

    String s = """
9,4, 11, 2, 40, 17,11,20,9,20, 15,13, 19, 35,12,14, 13,1, 0,20,9, 1
40,29, 40, 25, 30, 12, 31, 27,39,7,37,15,31,1,26, 36,36, 35,30, 5, 24
25, 9, 20,31, 18, 11, 33,22, 39,40, 4, 23,34, 13, 16, 19, 22,3, 36, 2 
18,23,16,2, 28,0, 9,15, 5,22, 35, 40,2,14,18, 33, 22,16,12, 35, 37,38,2
0,24, 29,37, 13,3, 9,18,39, 8,24,9,1,39,22,2, 27, 38,5, 38, 6, 11,13,10 
31,25,22, 16,2, 1,25, 26,30, 10, 36, 18,33,31, 27, 30,25, 12, 5,5,2, 35
7,7,36,16, 33,12,34, 19, 29, 40,5,7
""";
String[] stringArray = s.split("\\R|[\\s,]+");       
int count = 1;
for (String str : stringArray) {
System.out.print(str + " " );
if (count % 20 == 0) {
System.out.println();
}
count++;
}

prints no more than 20 per line to avoid horizontal scrolling.

9 4 11 2 40 17 11 20 9 20 15 13 19 35 12 14 13 1 0 20 
9 1 40 29 40 25 30 12 31 27 39 7 37 15 31 1 26 36 36 35 
30 5 24 25 9 20 31 18 11 33 22 39 40 4 23 34 13 16 19 22 
3 36 2 18 23 16 2 28 0 9 15 5 22 35 40 2 14 18 33 22 
16 12 35 37 38 2 0 24 29 37 13 3 9 18 39 8 24 9 1 39 
22 2 27 38 5 38 6 11 13 10 31 25 22 16 2 1 25 26 30 10 
36 18 33 31 27 30 25 12 5 5 2 35 7 7 36 16 33 12 34 19 
29 40 5 7 

If you want to convert them to an int array, do it like so. Note you need to eliminate white space to avoid a NumberFormatException.

int[] intArray = Arrays.stream(s.split("\\R|[\\s,]+"))
.mapToInt(Integer::parseInt).toArray();
</details>

huangapple
  • 本文由 发表于 2023年6月26日 05:11:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552425.html
匿名

发表评论

匿名网友

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

确定