How do I get a platform-dependent new line character?

前端 未结 9 1308
予麋鹿
予麋鹿 2020-11-22 02:17

How do I get a platform-dependent newline in Java? I can’t use \"\\n\" everywhere.

9条回答
  •  情歌与酒
    2020-11-22 02:35

    Avoid appending strings using String + String etc, use StringBuilder instead.

    String separator = System.getProperty( "line.separator" );
    StringBuilder lines = new StringBuilder( line1 );
    lines.append( separator );
    lines.append( line2 );
    lines.append( separator );
    String result = lines.toString( );
    

提交回复
热议问题