Using String Split in Java

In Java, the String class has a method called Split() designed to parse a given string wherever matches of the specified regular expression occur. The result of this method is an array of substrings based on matches of the passed regular expression. Additionally, you can add a second parameter called limit, which determines the number of potential uses of the patterns, subsequently affecting the resulting array's size. If the limit, denoted by n, is greater than zero, the pattern will be used a maximum of n - 1 times, the length of the resulting array will not exceed n, and the final array entry will encapsulate all input data after the last matching delimiter. In this Java String Split Example, we instantiate a string and split it into substrings wherever a comma is specified. Subsequently, we represent the separated substrings and determine the total number of these substrings. Click Execute to run the Java String Split Example online and see the result.
Using String Split in Java Execute
public class SplitExample {
    public static void main(String[] args) {
        String str = "apple,banana,grape";
        String[] fruits = str.split(",");
        for (String fruit : fruits) {
Updated: Viewed: 38 times