3

I have a below string which is coming from the server

String text = "- 30016264\n- 30014837\n- 30014836\n";

When I used to split it like this

String[] list = text.split("\n");

I got the list like this with length 1

list[0] = "- 30016264\n- 30014837\n- 30014836\n";

And when I used to split it like this

String[] list = text.split("\\n");

I got the same list like this with length 1

list[0] = "- 30016264\n- 30014837\n- 30014836\n";

How do I write the code to split the string on basis of "\n" not the next line?

NOTE: This string is coming from the server as it is written here and when I use this server string as TextView value, it will display in one single line.

Mohit Chauhan
  • 455
  • 1
  • 10
  • 20

1 Answers1

3

If you input is coming from the server and in this format :

- 30016264\n- 30014837\n- 30014836\n

Then, in Java it should be represented with double backslash like this :

- 30016264\\n- 30014837\\n- 30014836\\n

because backslash is a special character in Java, you have to escape it with another backslash.

Then to split with \\n you need to use \\\\n, why 4 backslashes because like i said before the backslash is special character for that you have to escape each one with another backslash for that you need 4 instead of 2 or 1.

Your solution should look like :

String text = "- 30016264\\n- 30014837\\n- 30014836\\n";
String[] split = text.split("\\\\n");

Outputs

- 30016264 
- 30014837
- 30014836
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • May i know the reason of four `/` ? – Vidhi Dave Mar 13 '18 at 09:10
  • It works. Thanks! – Mohit Chauhan Mar 13 '18 at 09:10
  • 2
    @VishvaDave each the input contain two backslash to you have to escape each one with backslash for that there are 4 backslashes – Youcef LAIDANI Mar 13 '18 at 09:11
  • 1
    @YCF_L Ok got it thanks! – Vidhi Dave Mar 13 '18 at 09:12
  • you are welcome @MA13 – Youcef LAIDANI Mar 13 '18 at 09:13
  • **Use This Simplest Way** String text = "- 30016264\n- 30014837\n- 30014836\n"; String[] items = text.split("\n"); for (String item : items) { System.out.println("string split is = " + item); } – matin sayyad Mar 13 '18 at 09:16
  • `String text = "- 30016264\\n- 30014837\\n- 30014836\\n";` ???? By Jove... I understand nothing of this! You changed the original string. It was `String text = "- 30016264\n- 30014837\n- 30014836\n";`. Which was perfectly valid. I wonder why OP accepted your answer. – greenapps Mar 13 '18 at 09:20
  • @greenapps the OP said **How do I write the code to split the string on basis of "\n" not the next line?** also **NOTE: This string is coming from the server as it is written here and when I use this server string as TextView value, it will display in one single line.** so the `\n` is considered as string and not break line, to consider it as a string you have to use double backslash not only one, I think the OP makes a typo in his/her question – Youcef LAIDANI Mar 13 '18 at 09:25
  • No. OP does not. OP starts with a perfectly valid string. The string comes from the server. No reason to change that. Your answer does not make sense. And the reaction of OP either. – greenapps Mar 13 '18 at 09:28
  • @greenapps I assume that the OP use `String text = "- 30016264\\n- 30014837\\n- 30014836\\n"; String[] list = text.split("\n");` for that he/she get `list[0] = "- 30016264\n- 30014837\n- 30014836\n";` [take a look at this demo](https://ideone.com/CPeu3j) – Youcef LAIDANI Mar 13 '18 at 09:31
  • @greenapps note that the OP said that the information come from the server so another reason to makes me assume that he/she don't mean `String text = "- 30016264\n- 30014837\n- 30014836\n";` but `String text = "- 30016264\\n- 30014837\\n- 30014836\\n";` with double backslash – Youcef LAIDANI Mar 13 '18 at 09:33
  • Wrong assumption. You clearly can see the string OP received from the server. Its a perfectly valid string. Yours by the way is nonsense. – greenapps Mar 13 '18 at 09:34
  • @greenapps beside if you print `System.out.println("- 30016264\n- 30014837\n- 30014836\n");` you will get multiple lines but if you print `System.out.println("- 30016264\\n- 30014837\\n- 30014836\\n");` you will get `- 30016264\n- 30014837\n- 30014836\n` in one line there are no break-line [take a loot at this demo](https://ideone.com/CwIrtv) – Youcef LAIDANI Mar 13 '18 at 09:36
  • 1
    Yes you are right about what is printed in both cases. With the first one nice lines. With the second one nonsense. – greenapps Mar 13 '18 at 09:39
  • @greenapps all this happen because it is a trick question :) – Youcef LAIDANI Mar 13 '18 at 09:42
  • @YCF_L My string is correct and your answer is also correct for my condition. In your demo, string value is generated by java, not coming from the server that's why your demo also works fine. – Mohit Chauhan Mar 13 '18 at 09:55
  • @YCF For your edit - No, my solution looks like this when I print `- 30016264\n- 30014837\n- 30014836\n` – Mohit Chauhan Mar 13 '18 at 10:02
  • @MA13 Your question is really unclear, in java if you want to print `\n` you have to use a string like this `String s = "\\n";` and not `String s = "\n";` you get it? – Youcef LAIDANI Mar 13 '18 at 10:12