String Class Tutorials

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

The split() method is a very useful tool for parsing strings. In the signature above, the first parameter is named regex and that is significant; regex is an abbreviation for regular expression and you will hear the term regex used quite often. Regular expressions deserve an entire mini-tutorial series on their own, so I will just use a basic regex parameter for this tutorial. The split() method is overloaded and has two different signatures. The split(String regex) returns an array of strings that are separated by a delimiter. For example,
String s = "Apple-Banana-Orange-Pear-Watermelon";
String fruits[] = s.split("-");
// parse strings in between the dash character
for(String temp : fruits ) {
        System.out.println(temp);
// display array elements to the console
}

The split(String regex, int limit) returns an array of strings that are separated by a delimiter, the second parameter specifies the max number of elements in the array. This method will most likely not work as you might expect, the last element of the array will contain all the rest of the entire string. For example,
String s = "Apple-Banana-Orange-Pear-Watermelon";
String fruits[] = s.split("-", 3);
// parse up to 3 strings in between the dash character
for(String temp : fruits ) {
        System.out.println(temp);
// display array elements to the console
}



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md StringSplit
C:\Java>cd StringSplit
C:\Java\StringSplit>Notepad StringSplit.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class StringSplit {
    public static void main(String args[]) {
        String s = "Apple-Banana-Orange-Pear-Watermelon";
        String fruits[] = s.split("-"); // parse strings in between the dash character
        for(String temp : fruits ) {
            System.out.println(temp); // display array elements to the console
        }

        System.out.println("\n----Max 3----");
        fruits = s.split("-", 3); // parse up to 3 strings in between the dash character
        for(String temp : fruits ) {
            System.out.println(temp); // display array elements to the console
        }

    }	
}


Now switch back to the command prompt (CMD) and type in javac StringSplit.java and press Enter.
Now type in java StringSplit and press Enter.


C:\Java\StringSplit>javac StringSplit.java
C:\Java\StringSplit>java StringSplit
Apple
Banana
Orange
Pear
Watermelon

----Max 3----
Apple
Banana
Orange-Pear-Watermelon


Final thoughts

One strategy that I regularly use to solidify my knowledge of Java is to browse through the actual source code. Due to some language in their copyright file, I can't actually show the source code in my videos. The source code is contained inside of a file called src.zip located in the folder where your JDK is installed. I recommend using notepad++ to browse the class files.


Tutorials