1 / 10

Strings

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

marge
Download Presentation

Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Strings Section 2.5.5 [Sikora] Chapter 3 (pp. 51-58) [Horstmann]

  2. 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

  3. 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()

  4. 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

  5. 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

  6. 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”

  7. 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")); } }

  8. 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)); } }

  9. 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)

  10. 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

More Related