Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中检查字符串是否包含回车符(CR)或换行符(LF)_Java_String_Smtp_Carriage Return - Fatal编程技术网

如何在Java中检查字符串是否包含回车符(CR)或换行符(LF)

如何在Java中检查字符串是否包含回车符(CR)或换行符(LF),java,string,smtp,carriage-return,Java,String,Smtp,Carriage Return,我有一个发送电子邮件的java方法。在将subject设置为mimessage的方法中,我需要检查subject字符串是否包含回车符(CR)或换行符(LF) //Set the From email address MimeMessage msg = new MimeMessage(session); //Set the Email Subject msg.setSubject(subject); 我想验证主题字符串是否包含回车符(CR)或换行符(LF)字符,因

我有一个发送电子邮件的java方法。在将subject设置为mimessage的方法中,我需要检查subject字符串是否包含回车符(CR)或换行符(LF)

//Set the From email address
        MimeMessage msg = new MimeMessage(session);
//Set the Email Subject
        msg.setSubject(subject);
我想验证主题字符串是否包含回车符(CR)或换行符(LF)字符,因为它可能是一种潜在威胁。

试试这个

if (subject.contains("\r") || subject.contains("\n")) // then invalid
试试这个

if (subject.contains("\r") || subject.contains("\n")) // then invalid