简体   繁体   English

将字符串数组拆分为另一个数组?

[英]Split a string array into another array?

I want to split a large text file into single words as I need to shuffle the letters of each word. 我想将一个大的文本文件拆分为单个单词,因为我需要对每个单词的字母进行混洗。

ReadFile file = new ReadFile(file_name);
String[] aryLines = file.OpenFile();

This is show I read in the textfile with text and gives an output of: 这表明我在文本文件中读取了文本,并给出了以下输出:

[This is Line One. , This is Line Two. , This is Line three. , End.]

How do I split this into {This, is, Line, One} etc? 如何将其拆分为{This,is,Line,One}等? I tried 我试过了

aryLines.split("\\s+");

but it doesnt work as aryLines is a array... 但是它不起作用,因为aryLines是一个数组...

    for (String string : arrLines) {
            string.split(",");
    }

you have and array, you just need to do a for each and split the content in each array you get. 您拥有and数组,您只需要为每个数组做一个,然后将内容拆分为每个数组即可。

I hope this help you. 希望对您有所帮助。

Given : 鉴于:

String[] aryLines = {
    "This is Line One.", "This is Line Two.", "This is Line three.", "End."
};

To obtain the result you're seeking, you need to split the content of the array, not the array itself : 为了获得您想要的结果,您需要拆分数组的内容,而不是数组本身:

ArrayList<List<String>> arrayList = new ArrayList<List<String>>();
for (String aString : aryLines) {
    arrayList.add(Arrays.asList(aString.split("\\s+")));
}

If you print arrayList it you'll get : 如果打印arrayList它会得到:

[[This, is, Line, One.], [This, is, Line, Two.], [This, is, Line, three.], [End.]]

Depending on how large the file is you could either read the file into a String then call split with a regex like 根据文件的大小,您可以将文件读取为字符串,然后使用正则表达式调用split

 string.split("(\\ )");

This would give you a String array with the words (and punctuation). 这将为您提供一个包含单词(和标点符号)的String数组。

Or if the file is very large you can read it line by line like you currently are and then split each line by iterating over it and adding the split words into a collection. 或者,如果文件很大,则可以像当前一样逐行读取它,然后通过迭代遍历并将拆分后的单词添加到集合中来拆分每一行。

ReadFile file = new ReadFile(file_name);
String[] aryLines = file.OpenFile();
List<String> words = new ArrayList<String>();
for (String line : aryLines) {
    for (String word : line.split("\\ ")) {
        words.add(word);
    }
}

Try this code: 试试这个代码:
Here, I am just getting the output for the first part, ie, "This is Line One." 在这里,我只是获得第一部分的输出,即“这是第一行”。 splitted and stored in array "aryLines1" as {This, is, Line, One.} 拆分并存储为数组“ aryLines1”,格式为{This,is Line,One。}。

public class TestingArray {

    public static void main(String[] args) throws IOException{


        File file  = new File("D:\\1-PROJECTS\\test.txt");
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        String s;

        List<String> list = new ArrayList();
        while((s=br.readLine())!=null){
            list.add(s);
        }

        String[] aryLines = list.toArray(new String[0]);    
        String[] aryLines1 = aryLines[0].split(" ");

        for(int i=0;i<aryLines1.length;i++){
            System.out.println(aryLines1[i].toString());
        }

    }

}

The Output comes out to be:- 输出结果是:
This 这个
is
Line 线
One. 一。

This is the content stored in array "aryLines1" . 这是存储在数组“ aryLines1”中的内容

Similarly you can split "aryLines" using (" ") and store in other arrays as well. 同样,您可以使用(“”)分割“ aryLines”并存储在其他数组中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM