Ads by ProfitSence
Close

Split a string with whitespace Java

Last Updated on Wednesday 5th Oct 2022

Java String Split Whitespace

The String split() in Java always accepts a special string something known as Regular Expression.

			
					split( String regularExpression )
split( String regularExpression, int limit )

			
	

Java Split Whitespace

  • \d Represents a digit: [0-9]
  • \D Represents a non-digit: [^0-9]
  • \s Represents a whitespace character including [ \t\n\x0B\f\r]
  • \S Represents a non-whitespace character as [^\s]
  • \v Represents a vertical whitespace character as [\n\x0B\f\r\x85\u2028\u2029]
  • \V Represents a non-vertical whitespace character as [^\v]
  • \w Represents a word character as [a-zA-Z_0-9]
  • \W Represents a non-word character as [^\w]

split by whitespace java

			
					String textStr[] = yourString.split("\\r?\\n");

			
	

Split on WhiteSpace java

			
					import java.util.Arrays;

class mainProg {
    public static void main(String[] args) {
        String str = "Hello   World";
        String res[] = str.split("\\s+");
        System.out.println( Arrays.toString( res ));
    }
}

			
	
			
					[Hello, World]

			
	

space delimiter java

			
					import java.util.Arrays;

class mainProg {
    public static void main(String[] args) {
        String theString = "Java<a space><a tab>Programming";
        String []allParts = theString.split("\\s+");
        System.out.println( Arrays.toString( allParts ));
    }
}

			
	
			
					[Java<a, space><a, tab>Programming]

			
	

References