Ram
Ram

Reputation: 45

how to put String values into new Line on getting spaces in java

Hii Guys !!! I have a string with values like 69 17 17 16 2 1 1 26 26 56 56 69 20 19 20 etc .Now As per my need i have to put these values into new String with each values in new line as after each value space is there ..

Any help will be highly appreciated.. Thanx in advance...

Upvotes: 1

Views: 782

Answers (2)

dvsander
dvsander

Reputation: 105

You should split the String using a specific separator into a List. Then print out the List using the format required. This helps when tomorow the String contains digits, decimals, text, etc or they want the text in another format.

    String source = "69 17 17 16 2 1 1 26 26 56 56 69 20 19 20";
    String[] splitted = source.split(" ");
    StringBuilder sb = new StringBuilder();
    for (String split : splitted){
        sb.append(split).append(System.getProperty("line.separator"));
    }
    System.out.println(sb.toString());

Upvotes: 0

urir
urir

Reputation: 2015

String newStr = origStr.replaceAll(" ", " \n");

Upvotes: 2

Related Questions