In this tutorial, you will learn about everything about java string split
In this tutorial we will learn about string split java method and its Syntax and Programming Examples. The split() in Java is a method that is used to break a string around the matches of the provided regular expression. The split() in Java is also used to break a string based on the provided string delimiter. The split() method in Java is invoked by a string and returns an array of strings.
Syntax :
The syntax of the string split() method is:
string.split(String regex, int limit)
Here, string is an object of the String class.
split() Parameters :
The string.split java method can take two parameters:
- regex – the string is divided at this regex (can be strings)
- limit (optional) – controls the number of resulting substrings
If the limit parameter is 0 or negative, split() returns an array containing all substrings.
If the limit parameter is positive (let’s say n), split() returns the maximum of n substrings.
If the limit parameter is not passed, split() returns all possible substrings.
There is only one question arises in mind i.e. how to split a string in java. For thisβ¦β¦β¦β¦β¦β¦β¦
Example :
public class StringSplitTest {
public static void main(String[] arg) {
String str = "Hello:world:hello";
String split[] = str.split("e", 5);
for (String s: split)
System.out.println(s);
}
}
Output :
H
llo:world:h
llo
Example 2-
public class StringSplitTest {
public static void main(String[] arg) {
String str = "What are you doing today?";
String split[] = str.split(" ", 0);
for (String s: split)
System.out.println(s);
}
}
Output :
What
are
you
doing
Today?
Syntax Briefly :
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.
public 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.
public 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
split() Return Value
returns an array of substrings
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.
More Related Post
- PHP For Loop
- PHP MVC Framework
- Login Page in PHP
- PHP String Replace Function
- Java String indexOf() Method
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.