More String Methods
Premium Content - Free Preview
Below are 3 additional String methods. After reviewing them, you should be able to solve the challenges and tasks below and the challenges in String Programming Practice.
indexOf(c) - Finds the character 'c' within the String and returns the index where 'c' first occurs. For example:
int value = "abcaz".indexOf('a');
value will equal 0, the index of the first 'a' in the String. This method can also be used to get the indexOf a substring:
value = "abcaz".indexOf("az");
This will set
3 since "az" starts at index 3.
replace(oldChar, newChar) - Returns a new string resulting from replacing all occurrences of
newChar. For example:
String verb = "food".replace('o', 'e');
verb will equal "feed". This method can also be used to replace Strings instead of characters: