68

How can I check to see if a String contains a whitespace character, an empty space or " ". If possible, please provide a Java example.

For example: String = "test word";

srujzs
  • 340
  • 3
  • 14
jimmy
  • 8,121
  • 11
  • 36
  • 40

14 Answers14

92

For checking if a string contains whitespace use a Matcher and call its find method.

Pattern pattern = Pattern.compile("\\s");
Matcher matcher = pattern.matcher(s);
boolean found = matcher.find();

If you want to check if it only consists of whitespace then you can use String.matches:

boolean isWhitespace = s.matches("^\\s*$");
user207421
  • 305,947
  • 44
  • 307
  • 483
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
33

Check whether a String contains at least one white space character:

public static boolean containsWhiteSpace(final String testCode){
    if(testCode != null){
        for(int i = 0; i < testCode.length(); i++){
            if(Character.isWhitespace(testCode.charAt(i))){
                return true;
            }
        }
    }
    return false;
}

Reference:


Using the Guava library, it's much simpler:

return CharMatcher.WHITESPACE.matchesAnyOf(testCode);

CharMatcher.WHITESPACE is also a lot more thorough when it comes to Unicode support.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
25

This will tell if you there is any whitespaces:

Either by looping:

for (char c : s.toCharArray()) {
    if (Character.isWhitespace(c)) {
       return true;
    }
}

or

s.matches(".*\\s+.*")

And StringUtils.isBlank(s) will tell you if there are only whitepsaces.

Ebbe M. Pedersen
  • 7,250
  • 3
  • 27
  • 47
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
12

Use Apache Commons StringUtils:

StringUtils.containsWhitespace(str)
br2000
  • 969
  • 11
  • 12
2

Use this code, was better solution for me.

public static boolean containsWhiteSpace(String line){
    boolean space= false; 
    if(line != null){


        for(int i = 0; i < line.length(); i++){

            if(line.charAt(i) == ' '){
            space= true;
            }

        }
    }
    return space;
}
olajide
  • 972
  • 1
  • 13
  • 26
Gilberto
  • 29
  • 1
2

You could use Regex to determine if there's a whitespace character. \s.

More info of regex here.

Sam
  • 7,252
  • 16
  • 46
  • 65
123 456 789 0
  • 10,565
  • 4
  • 43
  • 72
2
public static void main(String[] args) {
    System.out.println("test word".contains(" "));
}
hanumant
  • 1,091
  • 4
  • 15
  • 27
1

You can use charAt() function to find out spaces in string.

 public class Test {
  public static void main(String args[]) {
   String fav="Hi Testing  12 3";
   int counter=0;
   for( int i=0; i<fav.length(); i++ ) {
    if(fav.charAt(i) == ' ' ) {
     counter++;
      }
     }
    System.out.println("Number of spaces "+ counter);
    //This will print Number of spaces 4
   }
  }
C-Otto
  • 5,615
  • 3
  • 29
  • 62
Ahmed Tareque
  • 161
  • 5
  • 17
0
String str = "Test Word";
            if(str.indexOf(' ') != -1){
                return true;
            } else{
                return false;
            }
0

Maybe I'm late with the most updated answer. You can use one of the following solution:

public static boolean containsWhiteSpace(final String input) {
        if (isNotEmpty(input)) {
            for (int i = 0; i < input.length(); i++) {
                if (Character.isWhitespace(input.charAt(i)) || Character.isSpaceChar(input.charAt(i))) {
                    return true;
                }
            }
        }
        return false;
    }

or

public static boolean containsWhiteSpace(final String input) {
        return CharMatcher.whitespace().matchesAnyOf(input);
    }
logbasex
  • 1,688
  • 1
  • 16
  • 22
-1
import java.util.Scanner;
public class camelCase {

public static void main(String[] args)
{
    Scanner user_input=new Scanner(System.in);
    String Line1;
    Line1 = user_input.nextLine();
    int j=1;
    //Now Read each word from the Line and convert it to Camel Case

    String result = "", result1 = "";
    for (int i = 0; i < Line1.length(); i++) {
        String next = Line1.substring(i, i + 1);
        System.out.println(next + "  i Value:" + i + "  j Value:" + j);
        if (i == 0 | j == 1 )
        {
            result += next.toUpperCase();
        } else {
            result += next.toLowerCase();
        }

        if (Character.isWhitespace(Line1.charAt(i)) == true)
        {
            j=1;
        }
        else
        {
            j=0;
        }
    }
    System.out.println(result);
James A Mohler
  • 11,060
  • 15
  • 46
  • 72
-1

Use org.apache.commons.lang.StringUtils.

  1. to search for whitespaces

boolean withWhiteSpace = StringUtils.contains("my name", " ");

  1. To delete all whitespaces in a string

StringUtils.deleteWhitespace(null) = null StringUtils.deleteWhitespace("") = "" StringUtils.deleteWhitespace("abc") = "abc" StringUtils.deleteWhitespace(" ab c ") = "abc"

Khalil M
  • 1,788
  • 2
  • 22
  • 36
-1

I purpose to you a very simple method who use String.contains:

public static boolean containWhitespace(String value) {
    return value.contains(" ");
}

A little usage example:

public static void main(String[] args) {
    System.out.println(containWhitespace("i love potatoes"));
    System.out.println(containWhitespace("butihatewhitespaces"));
}

Output:

true
false
Valentin Michalak
  • 2,089
  • 1
  • 14
  • 27
-2
package com.test;

public class Test {

    public static void main(String[] args) {

        String str = "TestCode ";
        if (str.indexOf(" ") > -1) {
            System.out.println("Yes");
        } else {
            System.out.println("Noo");
        }
    }
}
zmb
  • 7,605
  • 4
  • 40
  • 55