BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Split String by space in Java

By Chaitanya Singh | Filed Under: Java Examples

In this guide, we will write a program to split a string by space in Java.

Java Program to split string by space

You can use \\s+ regex inside split string method to split the given string using whitespace. See the following example.

\s – Matches any white-space character. The extra backslash is to escape the sequence. The + at the end of \\s+ is to capture more than one consecutive spaces between words.

Complete Java code:

public class JavaExample{
  public static void main(String args[]){

    //a string with space
    String str = "This is just a text with space";

    //split the given string by using space as delimiter
    String[] strArray = str.split("\\s+");

    //prints substrings after split
    for(String s: strArray){
      System.out.println(s);
    }
  }
}

Output:

Split String by space example output

Splitting a string with Multiple Consecutive Spaces

You can also use the following regex inside the split method to split the given string.

str.split("[ ]")

However there is a difference between \\s+ and [ ] regex.

  • \\s+ : It consumes all the consecutive spaces and doesn’t produce empty substrings.
  • [ ] : It splits the string when the first space is encountered, which means if there are multiple consecutive spaces then it would produce empty strings. See the following example:
public class JavaExample{
  public static void main(String args[]){

    //multiple consecutive whitespaces
    String str = "Test   String";

    //split using \\s+ regex
    String[] strArray = str.split("\\s+");
    for(int i=0; i<strArray.length; i++){
      System.out.println("strArray["+i+"]"+": "+strArray[i]);
    }

    //split using [ ] regex
    String[] strArray2 = str.split("[ ]");
    for(int i=0; i<strArray2.length; i++){
      System.out.println("strArray2["+i+"]"+": "+strArray2[i]);
    }
  }
}

Output:

Split String by multiple spaces in Java

Related guides:

  • Split String by Comma
  • Split String by Pipe
  • Split String by Capital Letters
Main Article: String in Java

Programs

  • C Programs
  • Java Programs
  • C++ Programs

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap