Topic 1 - Variables and Data Types
Topic 2 - Conditionals and Strings
Topic 3 - Loops
Topic 4 - Arrays
Topic 5 - File Handling
Semester 1 Projects
Topic 6 - Classes/Objects and Methods
Topic 7 - ArrayLists
Semester Projects

String Methods

Apart from the equals() method, there are other useful methods that we can use to get information from strings.

indexOf()

Returns the index of the first occurrence of a character within this string. Returns -1 if the character is not found.

String name = "Hong Kong";
System.out.println(name.indexOf('H')); // this will print 0
System.out.println(name.indexOf('g')); // 'g' occurs twice, this will print 3 (first occurence)
System.out.println(name.indexOf('x')); // doesn't exist, this will print -1

Substring()

Returns a substring of the original string, starting at an index.
Note, index values begin at 0.
For example, if a string has n characters, the index of the first character is 0, and the index of the last character is n-1.

String name = "Hong Kong";
String newName =  name.substring(4); //this will be "Kong"

You can alternatively pick exactly where to start and end the substring. The ending index number is not inclusive, that is, the character at the ending index is not included in the substring that is returned.

String name = "Hong Kong";
String newName = name.substring(2, 7) // will result in "ng Ko"
String x = name.substring(3, 4) // will result in "g"

Using the substring method returns a new string. This method never changes the original string.

String name = "Hong Kong";
name.substring(3, 6); //this essentially does nothing

String x = name.substring(3, 6); // the new variable x has a value of "g K"

System.out.println(name); //will print "Hong Kong"
System.out.println(x); // will print "g K"

toUpperCase()

toLowerCase()

Creates a new string with all of the characters converted to lowercase or uppercase.

String name = "Hong Kong";

String allUpper = name.toUpperCase(); // "HONG KONG"
String allLower = name.toLowerCase(); // "hong kong"

name.toUpperCase(); //essentially does nothing, no variable saves the new string

charAt()

Returns the char value at a specified index.

String name = "Hong Kong";

char firstLetter = name.charAt(0);

System.out.println(firstLetter); //will print H

char randomLetter = name.charAt(20); //returns an error, the index is too big!

Here is a short video about the Java8 API, and how to find more information about the methods in the String class.