Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to achieve the a new line when when appending a content to StringBuffer object usint "\n"

Tags:

java

string

i have written method which takes the big string and breaks into two lines :

private String  getNameSplited(String name){
char[] sAr = name.toCharArray();

StringBuffer strBuff = new StringBuffer(name.length());
    boolean start = true;
    for (int i = 0; i < sAr.length; i++) {
        if(i > 20) {
            if(sAr[i] == ' ' && start){
                strBuff.append("\n");
                start = false;
            } else {
                strBuff.append(sAr[i]);
            }
        } else {
            strBuff.append(sAr[i]);
        }
    }
    return strBuff.toString();
}

In this method "\n" is not working. As in my project we are not using direct System.out.println();

we are using out.print(strBuff); to print this string in a dialog.

So can cany one give suggestion how to make this code work . Thanks you...

like image 867
Ashok Avatar asked Dec 05 '25 03:12

Ashok


1 Answers

\n might not be the newline character in the given system. Use System.getProperty("line.separator") to get the line separator corresponding to the runtime environment.

Use strBuff.append(System.getProperty("line.separator")); Or store System.getProperty("line.separator") as a constant and use it wherever you need a new line.

like image 57
Hari Menon Avatar answered Dec 06 '25 22:12

Hari Menon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!