Java - New Line Character Issue

后端 未结 4 810
半阙折子戏
半阙折子戏 2021-01-28 01:50

I have a code like,

String str = \" \" ; 

while(  cond ) {

  str = str + \"\\n\" ;

}

Now, I don\'t know why at the time of printing, the out

4条回答
  •  有刺的猬
    2021-01-28 02:28

    Looks like you are trying to run the above code on Windows. Well the line separator or new line is different on Windows ( '\r\n' ) and Unix flavors ('\n').

    So, instead of hard coding and using '\n' as new line. Try getting new line from the system like:

    String newLine = System.getProperty("line.separator");
    String str = " " ; 
    
    while(  cond ) {
    
      str = str + newLine ;
    
    }
    

提交回复
热议问题