Java String split() Method
Why we use strings in Java?
In Java, A group of characters known as a string class are ones that a script interprets literally. A string is represented as a byte (or term) range information structure in order to preserve a number of components, frequently letters. Additional types and arrangements of sequence (or array) data, such as more adaptable arrays, may also be designated by the sequence.
- Numerous methods, including compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), and substring(), are available in the Java String class.
To know more about String Class in java read the complete article.
String spilt () Method:
A Java String array is produced using the Java String split() function. It takes a string parameter and uses regular expression to process it. A char array is produced by the java string split() function after it separates the given string using a regular expression.
The java.lang.String class provides a lot of built-in methods that are used to manipulate string in Java.These methods help us to perform operations on String objects such as trimming, concatenating, converting, comparing, replacing strings etc.
- When coding, splitting strings is a pretty common procedure. The String split() function is the most popular technique to split a string in Java, however there are other methods as well.
Syntax:
public String[] split(String regex)
Parameters:
regex - the delimiting regular expression
Let’s look at a string-related Java program where the Java String spilt() Method is used to perform an operation on the given string.
Example: Java String Spilt Method
public class Main { public static void main(String[] args) { String s1="Welcome to PrepInsta and PrepInsta Prime"; String[] words=s1.split("\\s"); for(String w:words){ System.out.println(w); } }}
Output
Welcome to PrepInsta and PrepInsta Prime
Example 2 : Java Spilt Method
public class Main { public static void main(String[] args) { String str = "PrepInsta And Prime"; System.out.println("Returning words:"); String[] arr = str.split("P", 0); for (String w : arr) { System.out.println(w); } System.out.println("Split array length: "+arr.length); } }
Output
Returning words: repInsta And rime Split array length: 3
Example 3: Java Spilt Method Using Regex and Length
public class Main { public static void main(String[] args) { String str = "PrepInsta Prime"; System.out.println("Returning words:"); String[] arr = str.split("P", 0); for (String w : arr) { System.out.println(w); } System.out.println("Split array length: "+arr.length); } }
Output
Returning words: repInsta rime Split array length: 3
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment