21 String Class Methods char charAt int pos returns the character at position

21 string class methods char charat int pos returns

This preview shows page 21 - 31 out of 82 pages.

21 String Class Methods § char charAt (int pos) returns the character at position indicated by its argument pos . Note that the first character is at position 0 § Eg: String myStr = "Computer Science"; char ch = myStr.charAt(5); returns the character 't' in variable ch
Image of page 21
22 String Class Methods § String substring (int start, int end) returns the substring starting at position start and ending one character before position end § Eg: String sub1 = myStr.substring(2, 4); returns "mp" in String variable sub1 (assuming that myStr has the value “Computer Science”)
Image of page 22
23 String Class Methods § String substring (int start) returns the substring starting at position start of this string through to the end of the string § Eg: String sub2 = myStr.substring(9); returns "Science" in String variable sub2 (assuming that myStr has the value “Computer Science”)
Image of page 23
24 String Class Methods § boolean equals (String other) returns whether or not this String has the same value as the other String § Eg: s.equals("Hello") § boolean equalsIgnoreCase (String other) behaves like equals but regards upper and lower case versions of a letter to be the same
Image of page 24
25 String Class Methods § int compareTo (String other) compares this String to the other String and returns: § 0 if they have the same value § a negative number if this String comes before the other String in the lexicographic (dictionary) ordering § a positive number otherwise
Image of page 25
26 String Class Methods § Eg: System.out.println(“abc”.compareTo(“abc”) ); § // will output the value 0 System.out.println(“abc”.compareTo(“bac”) ); § // will output a negative number System.out.println(“xyz”.compareTo(“def”) ); § // will output a positive number § compareToIgnoreCase( other ) is also available, which compares two strings lexicographically, ignoring case differences
Image of page 26
27 String Class Methods § int indexOf (String other) returns the index of first occurrence of substring other within this String. Returns -1 if substring other is not found § String replace (char oldChar, char newChar) returns a new string having the same characters as this string, but with each occurrence of oldChar replaced by newChar § And many more (eg: toLowerCase(), toUpperCase(), lastIndexOf(String other), ... )
Image of page 27
28 String Class Methods § If you want to find out what methods are available (and exactly what they do, and how to call them, etc.) for the String class or any other Java library class then you can look up the Java on-line documentation § Java documentation is provided on the Web by Oracle at: and is also available on the lab machines
Image of page 28
29 String Class Methods § When you find the right Class in the right library you will get § an overview of the Class § a summary of the Methods and § a list of the details of the methods § It is strongly recommended that you become familiar with using the documentation
Image of page 29
30 // File: TestString.java class TestString { public static void main( String[] args ){ // str1 and str2 are variables referring to an object, // but the objects do not exist yet . String str1; String str2; // len1 + len2 are two primitive variables of type
Image of page 30
Image of page 31

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture