1

Which method of java.lang.String should I use to split the string below into an array or collection of strings, the splitting being done with respect to the character of the line break?

String str="This is a string\nthis is the next line.\nHello World."; I think that method is split().

String[] arrOfStr = str.split("\n", limit);

but I don't know what to put in the limit in order to follow the requirements.

I did that code:

public class Split {

    public static void main(String[] args) {
        String str="This is a string\nthis is the next line.\nHello World.";
        String[] arrOfStr = str.split("\n");
        System.out.println(arrOfStr);

    }

}

But the result is [Ljava.lang.String;@36baf30c I don't understand.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
La_Magie
  • 55
  • 1
  • 6
  • 1
    Its not necessary - there is a [one argument form](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-) and a [two argument form](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-int-). – costaparas Jan 10 '21 at 13:46
  • Also, this question should be tagged with [tag:java]. In future, please tag all questions with the language being used. – costaparas Jan 10 '21 at 13:48
  • And regarding the output from printing the array, see [this question](https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – Joakim Danielson Jan 10 '21 at 13:56

4 Answers4

1

Iterate through the array that you created from the split method.

public static void main(String[] args) {
    String str="This is a string\nthis is the next line.\nHello World.";
    String[] arrOfStr = str.split("\n");
    for(int i = 0; i < arrOfStr.length; i++){
        System.out.println(arrOfStr[i]);
    }
}

Also another way to look at the contents is to use Arrays.deepToString() such as:

System.out.println(Arrays.deepToString(arrOfStr));

This results in the following outputs:

for loop iterating output:

This is a string
this is the next line.
Hello World.

deepToString output:

[This is a string, this is the next line., Hello World.]
Jamin
  • 1,362
  • 8
  • 22
1

You don't need to pass a limit to String#split if you want to split for all occurrences of \n. To show the contents of the array (instead of the reference), you can use Arrays.toString.

String str = "This is a string\nthis is the next line.\nHello World.";

String[] arrOfStr = str.split("\n");

System.out.println(Arrays.toString(arrOfStr));

Output:

[This is a string, this is the next line., Hello World.]

To see what the limit argument does, you can write a for-loop and observe the result like this:

String str = "This is a string\nthis is the next line.\nHello World.";
for (int limit = 0; limit < 4; limit++) {
    String[] arrWithLimit = str.split("\n", limit);
    System.out.println(limit + ": " + Arrays.toString(arrWithLimit));
}

Output:

0: [This is a string, this is the next line., Hello World.]
1: [This is a string
this is the next line.
Hello World.]
2: [This is a string, this is the next line.
Hello World.]
3: [This is a string, this is the next line., Hello World.]

You can see that the parameter limits the amount of splits being applied. 0 considers all occurrences (same as without limit). With 1 there is only 1 element in arrWithLimit and with 2 there are 2 elements.

Matt
  • 12,848
  • 2
  • 31
  • 53
0
String str="This is a string\nthis is the next line.\nHello World.";
String ab[]=    str.split("[\\r\\n]+");
Arrays.stream(ab).forEach(abc -> System.out.println(abc + " "));
murari99732
  • 157
  • 1
  • 7
0

Replace in your code:

System.out.println(arrOfStr); // it is printing the memory location since the `toString` 
                              //   method is implemented in this way

by:

 for(int i = 0; i<arrOfStr.length; i++)
     System.out.println(arrOfStr[i]);
   }
tino89
  • 142
  • 5