简体   繁体   English

JSP中的空白

[英]white space in JSP

I want to know how to check if there is white space in a string or not in JSP. 我想知道如何检查JSP字符串中是否有空格。

EX : 例如:

String name = "Richard hailes"; 字符串名称=“ Richard hailes”;

I want to know if there is a space in above string or not. 我想知道上面的字符串中是否有空格。

<c:if test="${fn:contains(name, ' ')}">
  It contains a space
</c:if>

See https://stackoverflow.com/tags/jstl/info for info about the JSTL. 有关JSTL的信息,请参见https://stackoverflow.com/tags/jstl/info

use indexOf function. 使用indexOf函数。

if(name.indexOf(' ') >= 0){
   // name have space
}

see indexOf 参见indexOf

public static boolean isBlank(String str) { int strLen; public static boolean isBlank(String str){int strLen; if (str == null || (strLen = str.length()) == 0) { return true;} for (int i = 0; i < strLen; i++) { if ((Character.isWhitespace(str.charAt(i)) == false)) {return false;}}return true;} } if(str == null ||(strLen = str.length())== 0){返回true;} for(int i = 0; i <strLen; i ++){if(((Character.isWhitespace(str.charAt (i))==假)){返回假;}}返回真;}}

Use regular expression. 使用正则表达式。 See this sample; 看到这个例子;

    String patternStr = "\\s+";
    String inputStr = "Richard hailes";
    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(inputStr);
    if(matcher.find()) {
       System.out.println("Found");
     } else {
       System.out.println("Not Found");
     }

You can look at this page which describes how to use the contains function in JSP. 您可以查看此页面该页面描述了如何在JSP中使用contains函数。 You can use that to see if the string contains " ". 您可以使用它来查看字符串是否包含“”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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