简体   繁体   English

Java从数组中拆分字符串

[英]Java split string from array

I have a string array that contains some information. 我有一个包含一些信息的字符串数组。

Example: 例:

 String [] testStringArray;

 testStringArray[0]= Jim,35
                     Alex,45 
                     Mark,21

 testStringArray[1]= Ana,18
                    Megan,44

This is exactly how the information is. 信息就是这样。 Now my problem is I want to make each element a seperate element in an array and I want to split it based on the \\n character. 现在我的问题是我想使每个元素成为数组中的单独元素,并且想基于\\ n字符对其进行拆分。

So I want 所以我要

        newArray[0]=Jim,35
        newArray[1]=Alex,45
        newArray[2]=Mark,21
        newArray[3]=Ana,18

etc etc. I am aware of the split method but won't this just split each array element into a completely new array instead of combining them? 等等等等。我知道split方法,但是这不会只是将每个数组元素拆分成一个全新的数组,而不是将它们组合吗?

If anyone could help, it would be appreciated. 如果有人可以帮助,将不胜感激。 Thanks 谢谢

Something like this: 像这样:

    // Splits the given array of Strings on the given regex and returns
    // the result in a single array.
    public static String[] splitContent(String regex, String... input) {
        List<String> list = new ArrayList<>();
        for (String str : input) {
            for (String split : str.split(regex)) {
                list.add(split);
            }
        }
        return list.toArray(new String[list.size()]);
    }

you can call it this way: 您可以这样称呼:

    String[] testStringArray = ...;
    String[] newArray = splitContent("\n", testStringArray);

Because of the use of varargs you can also call it like this: 由于使用了varargs,您还可以这样称呼它:

    String[] newArray = splitContent("\n", str1, str2, str3, str4);

where strX are String variables. 其中strXString变量。 You can use any amount you want. 您可以使用任何数量的金额。 So either pass an array of Strings, or any amount of Strings you like. 因此,要么传递一个字符串数组,要么传递您喜欢的任意数量的字符串。

If you don't need the old array anymore, you can also use it like this: 如果您不再需要旧的数组,也可以这样使用它:

    String[] yourArray = ...;
    yourArray = splitContent("\n", yourArray);
    String[] testStringArray = new String[2];
    ArrayList<String> result = new ArrayList<String>();
    testStringArray[0]= "Jim,35\nAlex,45\nMark,21";
    testStringArray[1]= "Jiam,35\nAleax,45\nMarak,21";
    for(String s : testStringArray) {
        String[] temp = s.split("\n");
        for(String t : temp) {
            result.add(t);
        }
    }
    String[] res = result.toArray(new String[result.size()]);

you can first merge the strings into one string and then use the split method for the merged string. 您可以先将字符串合并为一个字符串,然后对合并的字符串使用split方法。

testStringArray[0]= Jim,35
                     Alex,45 
                     Mark,21

testStringArray[1]= Ana,18
                    Megan,44

StringBuffer sb = new StringBuffer();

for(String s : testStringArray){

    s = s.trim();
    sb.append(s);
    if (!s.endWith("\n")){
        sb.append("\n");
    }

}

String[] array = sb.toString().split("\n");

Try This is working Code >> 试试这是工作代码>>

String[] testStringArray = new String[2]; // size of array

    ArrayList<String> result = new ArrayList<String>();

    testStringArray[0]= "Jim,35\nAlex,45\nMark,21"; // store value
    testStringArray[1]= "Ana,18\nMegan,44";

    for(String s : testStringArray) {
        String[] temp = s.split("\n"); // split from \n
        for(String t : temp) {
            result.add(t);   // add value in result
            System.out.print(t);


        }
    }
    result.toArray(new String[result.size()]);

Try this. 尝试这个。 It is simple and readable. 它简单易读。

ArrayList<String> newArray = new ArrayList<String>(); 

for (String s : testStringArray) {
    newArray.addAll(Arrays.asList(s.split("\\n"));
}

Firstly, you can't write what you just did. 首先,你不能写你刚刚做的事情。 You made a String array, which can only contain Strings. 您创建了一个String数组,该数组只能包含String。 Furthermore the String has to be in markers "" like "some text here". 此外,字符串必须位于标记“”中,例如“此处的某些文本”。

Furthermore, there can only be ONE String at one place in the array like: 此外,在数组中的某个位置只能有一个String,例如:

 newArray[0] = "Jim";
newArray[1] = "Alex";

And NOT like: 而且不像:

newArray[0] = Jim;

And CERTAINLY NOT like: 而且肯定不喜欢:

// Here you're trying to put 2 things in 1 place in the array-index
newArray[0] = Jim, 35;

If you wan't to combine 2 things, like an name and age you have to use 2D array - or probably better in your case ArrayList. 如果您不希望将名称和年龄之类的两件事结合起来,则必须使用2D数组-在您的情况下,最好使用ArrayList。

Make a new class with following object: 使用以下对象创建新类:

  public class Person {

    String name;
    int age;

    public Person(String name, int age) {
    this.name = name;
    this.age = age;
    }
    }

And afterwards go to your class where you want to use the original array, and write: 然后转到您要使用原始数组的类,并编写:

ArrayList<Person> someNameOfTheArrayList = new ArrayList<Person>();

someNameOfTheArrayList.add(new Person("Jim", 32));
someNameOfTheArrayList.add(new Person("Alex", 22));

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

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