100 likes | 248 Views
Strings. Section 2.5.5 [Sikora] Chapter 3 (pp. 51-58) [Horstmann]. The String Class. String str1; str1 = “abc”; String str2 = “xyz”; String str1, str2; String str1 = “abc”, str2; Note: char char1=‘a’; // char uses apostrophes
E N D
Strings Section 2.5.5 [Sikora] Chapter 3 (pp. 51-58) [Horstmann]
The String Class String str1; str1 = “abc”; String str2 = “xyz”; String str1, str2; String str1 = “abc”, str2; Note: char char1=‘a’; // char uses apostrophes String s1 = “aaa”; // String uses quotations
String Class Methods • char charAt(int index) • int indexOf(String str) • int indexOf(String str, int fromIndex) • int lastIndexOf(String str) • int lastIndexOf(String str, int fromIndex) • int length()
Example String s1 = “How now brown cow”, s2; char ch1, ch2; int index; s2 = “Go Cubs”; ch1 = s1.charAt(2); // ch1 = ‘w’ starts at zero ch2 = s2.charAt(5); // ch2 = ‘b’ index = s1.indexOf(“ow”); // index = 1 Index = s1.indexOf(“ow”, index+1) // index = 5 index = s1.lastIndexOf(“ow”) // index = 15 index = s1.lastIndexOf(“ow”, index – 1) // index = 10 index = s2.length(); // index = 7 (not 6!) # of characters
String Class Methods • String toLowerCase() • Changes the string to all lower case • String toUpperCase() • Changes the string to all uppercase • String trim() • Removes any whitespace characters (spaces, \n ) at start and end of string • String substring(int beginIndex, int endIndex) • Selects the characters starting at the beginning index and ending at endIndex – 1 • Has endIndex – begIndex number of characters • String substring(int beginIndex) • Selects the characters starting at the beginning index to the end
String Class Methods • String concat(String value) • Adds the string in the argument to the end of the “calling” string • Example: String s1; String s2; s1 = “abc”; s2 = s1.concat(“def”) // s2 = “abcdec”
Example 2 import java.lang.*; public class Sample1 { public static void main (String[] args) { String s1, s2, s3; char ch1, ch2; int index; s1 = "Go Cubs"; s2 = "Beat Cardinals"; s3 = s1 + s1.substring(2, 3) + s1.substring(0,2); System.out.println(s3); s3 = s1.substring(0, 3) + s2.substring(5) + "! " + s2.substring(0, 5) + s1.substring(3) + "!"; s3 = s3.toUpperCase(); System.out.println (s3); System.out.println (s2 == "Beat Cardinals"); System.out.println (s2.equals("Beat Cardinals")); } }
Example 3 import java.lang.*; public class Sample2 { public static void main (String[] args) { String s1; s1 = "Hello"; System.out.println(s1.substring(0, 3) == "Hel"); System.out.println(s1.substring(0, 3).equals ("Hel")); System.out.println(s1.equalsIgnoreCase(“HELLO)); } }
Other Methods • eguals • Compares two Strings • if (s1.equals(s2)) • equalsIgnoreCase • Just what it says – compares two Strings but ignores the upper/lower case used in the Strings • if (s1.equalsIgnoreCase)
More Methods • compareTo • Compares two strings to determine which one is first (alphabetically/numerically – Ascii code) • if (s1.compareTo(s2)) • If s1 is equal to s2, the compareTo methods returns a value of 0 • If s1 is less than s2, the compareTo method returns a value less than 0 • If s1 is greater than s2, the compareTo method returns a value greater than 0