-1

Well, the piece of code in my class :

  String sentence = "1. John," +
            "2. Mark," +
            "3. Sam";

And is logical that I have this output :

1. John
2. Mark
3. Sam 

, so I need to replace all commas to line breaks ( \n ) ;

  • 1
    [`String::replace`](https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/lang/String.html#replace(char,char)), http://idownvotedbecau.se/noresearch/ – Turing85 Dec 30 '19 at 21:17
  • 2
    Does this answer your question? [replace String with another in java](https://stackoverflow.com/questions/5216272/replace-string-with-another-in-java) Please google before posting. – AndrewG Dec 30 '19 at 21:17

2 Answers2

2

Unless I have missed something, this is as simple as using replace to change the command toa newline character (\n):

String sentence = "1. John," +
                  "2. Mark," +
                  "3. Sam";
String newSentence = sentence.replace(",", "\n");

Depending upon environment, you may want to use a carriage-return\line-feed which would be "\r\n".

Here's a link to a few examples of using it.

Martin
  • 16,093
  • 1
  • 29
  • 48
0

You literally just replace , with \n:

String sentence = "1. John\n" +
        "2. Mark\n" +
        "3. Sam";
pdrersin
  • 311
  • 3
  • 10