Free Java Tutorials >> Table of contents >> String Functions

1. String Arrays, split

Array length vs String length

It is easy to confuse array.length, which is a primitive, to string.length(), which is a function. Let's see an example where we sum up the lengths of multiple words:

public class MyProgram {
    public static void main(String[] args) {
		String[] words = {"a","cat","in","the","hat"};
		//No parenthesis for array length:
		System.out.println(words.length); //5
		System.out.println(words[0]);     //a
		
		//These extra parenthesis at the end of length are required:
		System.out.println(words[0].length()); //1
		
		//Let's sum up all the lengths:
		int sum = 0;
		for(int i = 0 ; i < words.length; i++) {
			sum += words[i].length();
		}
		System.out.println("The sum length is: "+sum); //12
    }
}

Take great care to know what the type of everything is, that way you wont mix up the two lengths.

The string .split function

The split function allows you to convert a String into an array of Strings. It is particularly useful for converting a sentence with multiple words into an array containing each word. This is best illustrated with an example: "

public class MyProgram {
    public static void main(String[] args) {
		String x = "necessity is the mother of invention";
		String[] words = x.split(" ");  //Every string with spaces in between converts into a new String
		//Now words is the array {"necessity","is","the","mother","of","invention"}
		System.out.println(words[0]);              //prints "necessity"
		System.out.println(words[1]);              //prints "is"
		System.out.println(words[words.length-1]); //prints "invention"
    }
}

We could use split, for example, to print the number of characters each word has:

public class MyProgram {
    public static void main(String[] args) {
		String x = "necessity is the mother of invention";
		String[] words = x.split(" ");  //Every string with spaces in between converts into a new String
		for(int i = 0 ; i < words.length; i++) {
			System.out.println(words[i]+" has "+words[i].length()+" characters");
		}
		//necessity has 9 characters
		//is has 2 characters
		//the has 3 characters
		//mother has 6 characters
		//of has 2 characters
		//invention has 9 characters
    }
}

2. Substring

The substring(int i, int j) function

The substring function allows you to get part of a string. For example, if we do println("tiger".substring(0,3)), we get the string "tig". Specifically, the first number represents the starting position of the substring. So if you choose 0, then the substring starts at the first character. Choosing 1 starts the substring at the second character, etc. The second number represents the position before the ending character. So if you have 3 as the second number, then we end at index 2. If we have 100 as the second number, we end at index 99. Here are some more examples:

public class MyProgram {
    public static void main(String[] args) {
		System.out.println("watermelon".substring(1,5)); //prints "ater"
		String horcrux = "soul";
		System.out.println(horcrux.substring(2,3));      //prints "r"
    }
}

Another way of thinking about the second number is: the length of the substring is the second minus the first. For example "hello12345".substring(5,8) starts at index 5, which is "1", and has a length of 8-5 which is 3. Thus, the resulting substring is "123"

Note: if we omit the second argument, substring starts at the first number and just goes to the end of the string

3. indexOf

The indexOf(String substr) function

The indexOf function can check whether a string is within another string. Additionally, the indexOf will also find the position of the substring in the larger string. For example, we can check if "wombat" contains "bat" (it does), and we can find that it is at position 3:

public class MyProgram {
    public static void main(String[] args) {
		String animal = "wombat";
		System.out.println(animal.indexOf("bat"));
    }
}

The lastIndexOf function

If there are multiple matches, the lastIndexOf is useful for finding the last match

public class MyProgram {
    public static void main(String[] args) {
		String words = "cat in the hat";
		System.out.println(words.indexOf("at"));     //1
		System.out.println(words.lastIndexOf("at")); //12
    }
}

4. toLowerCase, replace

The toLowerCase and toUpperCase functions

toUpperCase can be used to convert lower case letters like 'a' to 'A'. toLowerCase does the reverse. They are simple to use:

public class MyProgram {
    public static void main(String[] args) {
		System.out.println("YuNogETAPLUSplus".toUpperCase()); //prints "YUNOGETAPLUSPLUS"
		System.out.println("YuNogETAPLUSplus".toLowerCase()); //prints "yunogetaplusplus"
    }
}

The replace function

The replace function changes all occurrences of a substring to something else. For example:

public class MyProgram {
    public static void main(String[] args) {
		String quote = "It was the best of times, it was the worst of times";
		System.out.println(quote.replace("times","swims"));
		//Prints: It was the best of swims, it was the worst of swims
		
		//Notice that replace does not modify "quote"
		//Strings are "immutable" in Java:
		System.out.println(quote);
    }
}

Previous: Strings And Chars

Next: Function Declarations