String Methods
Java also has a variety of methods you can use on Strings to manipulate them: https://www.w3schools.com/java/java_ref_string.asp
You should be familiar with the following methods:
- charAt()
- compareTo()
- compareToIgnoreCase()
- contains()
- equals()
- equalsIgnoreCase()
- indexOf()
- isEmpty()
- lastIndexOf()
- length()
- replace()
- split()
- substring()
- toCharArray()
- toLowerCase()
- toUpperCase()
- trim()
Remember that because Strings are immutable, calling a String method will not necessarily change the original String.
Here is example code to test all of the methods listed above that you should be familiar with:
public static void main(String[] args) { String[] str = { "Tahiti, it's a magical place", "May the Force be with you", "no mourners no funerals", "apples", "oranges", "APPLES", " lots of whitespace ", }; System.out.println("-------------- Strings --------------"); for (int i = 0; i < str.length; i++) { System.out.println("str[" + i + "]: " + str[i]); } // chatAt() // prints each character in str[0] System.out.println("-------------- chatAt --------------"); for (int i = 0; i < str[0].length(); i++) { System.out.println(str[0].charAt(i)); } // compareTo() // returns a positive number if greater than // returns 0 if equal // returns a negative number if less than System.out.println("-------------- compareTo --------------"); System.out.println(str[3].compareTo(str[4])); // compareToIgnoreCase // same as above except case insensitive System.out.println("-------------- compareToIgnoreCase --------------"); System.out.println(str[3].compareToIgnoreCase(str[5])); // contains() System.out.println("-------------- contains --------------"); System.out.println(str[2].contains("mourners")); // equals() System.out.println("-------------- equals --------------"); System.out.println(str[0].equals(str[1])); // equalsIgnoreCase() System.out.println("-------------- equalsIgnoreCase --------------"); System.out.println(str[3].equalsIgnoreCase(str[5])); // indexOf() System.out.println("-------------- indexOf --------------"); System.out.println(str[0].indexOf("Tahiti")); System.out.println(str[0].indexOf('m')); // isEmpty() System.out.println("-------------- isEmpty --------------"); System.out.println(str[1].isEmpty()); System.out.println("".isEmpty()); // lastIndexOf() System.out.println("-------------- lastIndexOf --------------"); System.out.println(str[2].lastIndexOf("no")); System.out.println(str[2].lastIndexOf('u')); // length() System.out.println("-------------- length --------------"); System.out.println(str[3].length()); // replace() System.out.println("-------------- replace --------------"); System.out.println(str[0].replace('i', '5')); // split() System.out.println("-------------- split --------------"); String[] words = str[1].split(" "); for (String w : words) { System.out.println(w); } // substring() System.out.println("-------------- substring --------------"); System.out.println(str[3].substring(0, 2)); // toCharArray() System.out.println("-------------- toCharArray --------------"); char[] characters = str[4].toCharArray(); for (char c : characters) { System.out.println(c); } // toLowerCase() System.out.println("-------------- toLowerCase --------------"); System.out.println(str[0].toLowerCase()); // toUpperCase() System.out.println("-------------- toUpperCase --------------"); System.out.println(str[2].toUpperCase()); // trim() System.out.println("-------------- trim --------------"); System.out.println("Original string: " + str[6]); System.out.println("Updated string: " + str[6].trim()); // immutable System.out.println( "-------------- Strings are immutable --------------" ); for (int i = 0; i < str.length; i++) { System.out.println("str[" + i + "]: " + str[i]); } }
Output for the code above:
----------------- Strings ---------------- str[0]: Tahiti, it's a magical place str[1]: May the Force be with you str[2]: no mourners no funerals str[3]: apples str[4]: oranges str[5]: APPLES str[6]: lots of whitespace ----------------- CharAt ------------------ T a h i t i i t ' s a m a g i c a l p l a c e ---------------- compareTo -------------------- -14 ---------------- compareToIgnoreCase --------- 0 ---------------- contains -------------------- true ----------------- equals --------------------- false ----------------- equalsIsIgnoreCase --------- true ----------------- indexOf -------------------- 0 15 ----------------- isEmpty -------------------- false true ----------------- lastIndexOf ---------------- 12 16 ----------------- length --------------------- 6 ----------------- replace -------------------- tah5t5, 5t's a mag5cal place ----------------- split ---------------------- May the Force be with you ----------------- substring ------------------- ap ----------------- toCharArray ----------------- o r a n g e s ----------------- toLowerCase ----------------- tahiti, it's a magical place ----------------- toUpperCase ----------------- NO MOURNERS NO FUNERALS -------------- trim --------------------------- Orginal string: lots of whitespace Updated string: lots of whitespace --------------- Strings are inmutable --------- str[0]: Tahiti, it's a magical place str[1]: May the Force be with you str[2]: no mourners no funerals str[3]: apples str[4]: oranges str[5]: APPLES str[6]: lots of whitespace
Practice
Palindrome
Create a class called Palindrome. In the main method, prompt the user to enter a word. Then print whether the word given is a palindrome.
Note: Your program should disregard all leading and trailing whitespace, and should be case insensitive.
Example output 1:
Enter a word: RaCecAr The word is a palindrome.
Example output 2:
Enter a word: hello The word is NOT a palindrome.
Previous Section
10.4 The Math ClassNext Section
10.6 Chapter 10 ProjectCopyright © 2021 Code 4 Tomorrow. All rights reserved.
The code in this course is licensed under the MIT License.
If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.