How to find a space in a line?

How to check if a String string contains a space, blank space or "". If possible, provide a Java example.

For example: String = "test word";

+51
java string space
Nov 01 '10 at 9:38
source share
14 answers

To check if a string contains spaces, use 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 consists only of spaces, you can use String.matches :

 boolean isWhitespace = s.matches("^\\s*$"); 
+81
Nov 01 '10 at 9:41
source share

Check if String contains at least one 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 is much simpler:

 return CharMatcher.WHITESPACE.matchesAnyOf(testCode); 

CharMatcher.WHITESPACE also much more thorough when it comes to Unicode support.

+22
Nov 01 '10 at 9:51 on
source share

This will tell if you have spaces:

Or by cyclization:

 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 white spaces.

+19
Nov 01 '10 at 9:43 a.m.
source share

Use Apache Commons StringUtils :

 StringUtils.containsWhitespace(str) 
+8
Mar 15 '15 at 21:40
source share

Use this code was the best 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; } 
+3
Aug 17 '12 at 6:21
source share

You can use Regex to determine if there is a whitespace character. \s .

Read more about regex here .

+2
Nov 01 '10 at 9:41
source share
 public static void main(String[] args) { System.out.println("test word".contains(" ")); } 
+2
Nov 01 '10 at 9:41
source share
 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); 
0
Feb 16 '16 at 19:54
source share

Use org.apache.commons.lang.StringUtils.

  • to search for spaces

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

  1. To remove all spaces in a line

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

0
Apr 04 '16 at 2:36
source share
 String str = "Test Word"; if(str.indexOf(' ') != -1){ return true; } else{ return false; } 
0
Oct 28 '17 at 4:02 on
source share

I intend to give you a very simple method that uses String.contains :

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

A small usage example:

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

Exit:

 true false 
0
Jan 08 '18 at 14:10
source share

You can basically do it

 if(s.charAt(i)==32){ return true; } 

You must write a boolean method. The space character is 32.

0
Jan 08 '18 at 15:22
source share

You can use the chatAt () function to find spaces in a 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 } } 
0
Mar 31 '19 at 6:24
source share
 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"); } } } 
-one
Mar 16 '15 at 11:41
source share



All Articles