将字符串的单词转换为字符串数组[重复]

时间:2021-01-29 15:45:46

This question already has an answer here:

这个问题在这里已有答案:

Imagine i have the following String:

想象一下,我有以下字符串:

hoi
hoe 
gaat 
het

i get this String from a file.

我从一个文件中获取此字符串。

how can i make this in to a sting array like this:

我怎么能把这个变成这样的刺痛数组:

String[] hallo = {
        "hoi","hoe","gaat","het"        
        };

What would be the most simple way to achieve this?

实现这一目标最简单的方法是什么?

6 个解决方案

#1


6  

This works on any system.

这适用于任何系统。

String[] array = str.split(System.getProperty("line.separator"));

#2


6  

You can use split().

你可以使用split()。

String s[] = input.split("\n"); // "\n" if it is only a new-line. "\r\n" if you use windows OS.

#3


5  

From Java 8, there are some built-in methods for that :

从Java 8开始,有一些内置方法:

Files.readAllLines(Path)

it gives a List<String>. You can then convert it to an array if you really need to.

它给出了一个List 。如果确实需要,可以将其转换为数组。

Files.lines(Path)

it gives a Stream<String>. You can then handle each String without having to load the whole file in memory.

它给出了一个Stream 。然后,您可以处理每个String,而无需将整个文件加载到内存中。

#4


3  

Just another approach. Split based on anything which isn't between [a-zA-Z]. Works across platforms :

只是另一种方法。根据[a-zA-Z]之间的任何内容进行拆分。跨平台工作:

public static void main(String[] args) throws IOException, InterruptedException {
    String s = "asda\r\nasdsa";
    System.out.println(Arrays.toString(s.split("[^a-zA-Z]+")));
}

O/P :

O / P:

[asda, asdsa]

#5


0  

    try {
        File file = new File("file.txt");
        Path path = file.toPath();
        List<String> strings = Files.readAllLines(path); // this is available on **java 8**
     // List<String> strings = FileUtils.readLines(file); // this can be used with **commons-io-2.4 library** 
        for (String s : strings) {
            System.out.println(s);
        }
        String[] string_array = new String[strings.size()];
        for (int i = 0; i < strings.size(); i++) {
            string_array[i] = strings.get(i);
        }
        System.out.println(Arrays.deepToString(string_array));

    } catch (Exception e) {
        e.printStackTrace();
    }

#6


0  

If the input is in one entire string, you can do this to split the string into array of strings :

如果输入在一个完整的字符串中,您可以这样做以将字符串拆分为字符串数组:

String[] hallo = input.split("[ ]+");

#1


6  

This works on any system.

这适用于任何系统。

String[] array = str.split(System.getProperty("line.separator"));

#2


6  

You can use split().

你可以使用split()。

String s[] = input.split("\n"); // "\n" if it is only a new-line. "\r\n" if you use windows OS.

#3


5  

From Java 8, there are some built-in methods for that :

从Java 8开始,有一些内置方法:

Files.readAllLines(Path)

it gives a List<String>. You can then convert it to an array if you really need to.

它给出了一个List 。如果确实需要,可以将其转换为数组。

Files.lines(Path)

it gives a Stream<String>. You can then handle each String without having to load the whole file in memory.

它给出了一个Stream 。然后,您可以处理每个String,而无需将整个文件加载到内存中。

#4


3  

Just another approach. Split based on anything which isn't between [a-zA-Z]. Works across platforms :

只是另一种方法。根据[a-zA-Z]之间的任何内容进行拆分。跨平台工作:

public static void main(String[] args) throws IOException, InterruptedException {
    String s = "asda\r\nasdsa";
    System.out.println(Arrays.toString(s.split("[^a-zA-Z]+")));
}

O/P :

O / P:

[asda, asdsa]

#5


0  

    try {
        File file = new File("file.txt");
        Path path = file.toPath();
        List<String> strings = Files.readAllLines(path); // this is available on **java 8**
     // List<String> strings = FileUtils.readLines(file); // this can be used with **commons-io-2.4 library** 
        for (String s : strings) {
            System.out.println(s);
        }
        String[] string_array = new String[strings.size()];
        for (int i = 0; i < strings.size(); i++) {
            string_array[i] = strings.get(i);
        }
        System.out.println(Arrays.deepToString(string_array));

    } catch (Exception e) {
        e.printStackTrace();
    }

#6


0  

If the input is in one entire string, you can do this to split the string into array of strings :

如果输入在一个完整的字符串中,您可以这样做以将字符串拆分为字符串数组:

String[] hallo = input.split("[ ]+");