简体   繁体   中英

Java, how could we trim carriage return and line break?

Hello :

I am developing a web app using Java and beans.

I am trying to do a converter which checks if a message which the user introduces using a textarea is less than 5 characters long.

The difficulty is that event if the text area is empty it stills having /r/n and some spaces after it.

I have alreaady tried:

@Stateless
public class ComentarioNota {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    public String convierteComentarioNota(String evaluacion, String comentario) {
        if (evaluacion.trim().equals("Apto") && comentario != null && comentario.length() > 5) {
            return "Apto";
        } else {
            return "No Apto";
        }
    }
}

And when debbugging we can see the comment value in the comentario parameter:

在此处输入图片说明

Any help would be appreciated.

You can remove the linebreaks before the trim. How to remove line breaks from a file in Java?

How to remove newlines from beginning and end of a string (Java)?

text = text.replace("\n", "").replace("\r", "");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM