Java Strings: Searching Strings
There are a number of methods that search string to determine if a string starts or ends with a particular character or set of characters, or determine if a certain character is at a specific location, etc. These are:
- The startsWith( ) method that will return true if the argument matches the relevant start character(s) of a given string.
- The endsWith( ) method that will return true if the argument matches the relevant end character(s) of a given string.
- The charAt( ) method that returns the character at a specified index position within a string.
- The indexOf( ) method that returns the index position of a specified character within a string.
startsWith() method & endsWith() Methods
These methods will return true if their argument matches the relevant start or end character(s) of a given string. They are useful when you need to check a string before you use it.
Using the the string schoolString = “Learning Glue” let’s check to see if it starts with Learning or Glue.
String schoolString = "Learning Glue"; System.out.println("Our string is: " + schoolString); //Check if the string starts with "Learning" System.out.println("Does it start with Learning: " + schoolString.startsWith("Learning")); //Check if the string starts with "Glue" System.out.println("Does it start with Glue: " + schoolString.startsWith("Glue"));
Since these methods will return a boolean we can expect a true or false to our questions. The output should be:
run: Our string is: Learning Glue Does it start with Learning: true Does it start with Glue: false BUILD SUCCESSFUL (total time: 0 seconds)
charAt() Method
Since a string is essentially a collection of individual characters, or an array of chars, we can pinpoint any individual character by specifying its index. We can do this using the charAt( ) method which allows us to specify an index position within a string to return that character. The general syntax is:
string.charAt(num);
Using our schoolString variable (“Learning Glue”), let’s see this method in action:
String schoolString = "Learning Glue"; //Returns the first character char ch1 = schoolString.charAt(0); //Returns the third character char ch2 = schoolString.charAt(3); //Returns the fifth character char ch3 = schoolString.charAt(5); //Returns the tenth character char ch4 = schoolString.charAt(10); System.out.println("Our string is: " + schoolString); System.out.println("The character at index 0 is: " + ch1) System.out.println("The character at index 3 is: " + ch2) System.out.println("The character at index 5 is: " + ch3) System.out.println("The character at index 10 is: " + ch4);
The output returns the character at each index location within the string:
run: Our string is: Learning Glue The character at 0 index is: L The character at index 3 is: r The character at index 5 is: i The character at index 10 is: l BUILD SUCCESSFUL (total time: 0 seconds)
indexOf( ) Method
The indexOf( ) method will return the index position of the first occurrence of a character or the starting index position of a string. The general syntax for a char is:
string.indexOf(‘char’);
or for a string:
string.indexOf(“string”);
The indexOf( ) method is case sensitive, so “glue” with a lower case g is very different from “Glue” with an uppercase G. Using our schoolString variable (“Learning Glue”), let’s see this method in action:
String schoolString = "Learning Glue"; //Finds the index position of the first instance of the character 'e' int charLocation = schoolString.indexOf('G'); //Finds the index position of the first instance of the string 'Glue' int stringLocation = schoolString.indexOf("Glue"); //Finds the index position of the first space int spaceLocation = schoolString.indexOf(' '); //Outputs the string and index locations of the above characters and string System.out.println("Our string is: " + schoolString); System.out.println("The index of the first char G is: " + charLocation); System.out.println("The index of the string Glue is: " + stringLocation); System.out.println("The index of the first space is: " + spaceLocation);
The output returns the index location for the first occurrence of the character ‘G’, the string “Glue” and the first space in the string “Learning Glue”:
run: Our string is: Learning Glue The index of the first char G is: 9 The index of the first string Glue is: 9 The index of the first space is: 8 BUILD SUCCESSFUL (total time: 0 seconds)
The index position of any character or string of characters can be found using the indexOf( ) method.
Remember that the indexOf( ) method is case sensitive and index positions always start from 0.
If the indexOf( ) method cannot find a character or string, it will return -1.