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.
Returns the array of strings computed by splitting this string around matches of the given regular expression
Throws: PatternSyntaxException - if the regular expression's syntax is invalid
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
Explanation:Here the output comes through spliting the string based on whitespace
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
Explanation:Here, the output delivers an array of strings that was created by segmenting this string based on where the provided regular expression was found to match this string.
Example 3: Java Spilt Method Using Regex and Length
Login/Signup to comment