Nash
Nash

Reputation: 131

white space in JSP

I want to know how to check if there is white space in a string or not in JSP.

EX :

String name = "Richard hailes";

I want to know if there is a space in above string or not.

Upvotes: 1

Views: 2323

Answers (5)

Sai Ye Yan Naing Aye
Sai Ye Yan Naing Aye

Reputation: 6740

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");
     }

Upvotes: 1

Nithi
Nithi

Reputation: 166

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;} }

Upvotes: 1

Farnabaz
Farnabaz

Reputation: 4066

use indexOf function.

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

see indexOf

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691625

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

See https://stackoverflow.com/tags/jstl/info for info about the JSTL.

Upvotes: 3

Eivind Eidheim Elseth
Eivind Eidheim Elseth

Reputation: 2354

You can look at this page which describes how to use the contains function in JSP. You can use that to see if the string contains " ".

Upvotes: 0

Related Questions