split string java

Java String Split

The split word indicates divide particular word into smaller words. In java Language, we have an inbuilt Split method of String Class. So, we can easily use split Method. This Method has the functionality to split the String-based of some delimiter.

But split method splits a string into an array of sub strings given a specific delimiter. It returns the array of String. Because returns type is array of string.

Java String Split Syntax

public String[] split(String regex);
public String[] split(String regex, int limit);

In Java, String class has two types of Split function. Because split method is overloading.

1 ) public String [] split(String regex): Accept only Regular Expression.
2 ) public String [] split(String regex, int limit): Accept two parameters. One Regular Expression and another one is Limit.

1 ) String regex : regex means that particular value, from which point we want to break give String. so it will be break from given string.

Example: In the following String, we want to break from ” , “. so it will be break from given string.

String fullStr = “JAVA,PHP,WORDPRESS,MYSQL,DATABASE”;
fullStr.split(“,”);      // It will returns the array of sub strings. 

class SplitExample{
  public static void main(String []args){
    String fullStr = "JAVA,PHP,WORDPRESS,MYSQL,DATABASE";
    String[] arrSplit = fullStr.split(",");
    for (int i=0; i < arrSplit.length; i++){
      System.out.println(arrSplit[i]);
    }
  }
}

Output: 

JAVA
PHP
WORDPRESS
MYSQL
DATABASE

2 ) int limit : limit is optional parameter of split method. Because it indicates the total elements of given String.

class SplitExample{
  public static void main(String []args){
    String fullStr = "JAVA,PHP,WORDPRESS,MYSQL,DATABASE";
    String[] arrSplit = fullStr.split(",",2);
    for (int i=0; i < arrSplit.length; i++){
      System.out.println(arrSplit[i]);
    }
  }
}

Output: 

JAVA
PHP,WORDPRESS,MYSQL,DATABASE

More Related Post

Thanks for the reading post. I hope you like and understand the post. If you have any doubt regarding this post please comment below. Because it helps to improve content quality.

Leave a comment

Your email address will not be published. Required fields are marked *

0 0 0
FOLLOW ON