Java String new line

前端 未结 17 834
情歌与酒
情歌与酒 2020-11-28 23:17

I have string like

\"I am a boy\".

I want to print like this way

\"I 
am 
a
boy\".

Can anybody help me?<

17条回答
  •  执念已碎
    2020-11-28 23:30

    What about %n using a formatter like String.format()?:

    String s = String.format("I%nam%na%nboy");
    

    As this answer says, its available from java 1.5 and is another way to System.getProperty("line.separator") or System.lineSeparator() and, like this two, is OS independent.

提交回复
热议问题