CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Dates

Dates and Strings
String class
Methods

Methods for manipulating strings

Method Description
length() Returns an int value for the number of characters in this string.
indexOf(String) Returns an int value for the index of the first occurrence of the specified string in this string. Returns -1 if not found.
indexOf(String, startIndex) Returns an int value for the index of the first occurrence of the specified string starting at the specified index. Returns -1 if not found.
lastIndexOf(String) Returns an int value for the index of the last occurrence of the specified string in this string.
lastIndexOf(String, startIndex) Returns an int value for the index of the last occurrence of the specified string in this string starting at the specified index.
trim() Returns a String object with any spaces removed from the beginning and end of this string.
substring(startIndex) Returns a String object that starts at the specified index and goes to the end of the string.
substring(startIndex, endIndex) Returns a String object that starts at the specified start index and goes to, but doesn't include, the end index.
replace(oldChar, newChar) Returns a String object that results from replacing all instances of the specified old char value with the specified new char value.
split(delimiter) Returns an array of String objects that were separated in the original string by the specified delimiter.
charAt(index) Returns the char value at the specified index.

Finding String Length

Finding string length using the length() method:

message = "Welcome";
message.length() (returns 7)

Retrieving Individual Characters in a String

  • Do not use message[0]
  • Use message.charAt(index)
  • Index starts from 0

String Concatenation

String s3 = s1.concat(s2);
String s3 = s1 + s2;

s1 + s2 + s3 + s4 + s5 same as
(((s1.concat(s2)).concat(s3)).concat(s4)).concat(s5);

Extracting Substrings

You can extract a single character from a string using the charAt method. You can also extract a substring from a string using the substring method in the String class.

String s1 = "Welcome to Java";
String s2 = s1.substring(0, 11) + "HTML";

Finding a Character or a Substring in a String

"Welcome to Java".indexOf('W') returns 0.
"Welcome to Java".indexOf('x') returns -1.
"Welcome to Java".indexOf('o', 5) returns 9.
"Welcome to Java".indexOf("come") returns 3.
"Welcome to Java".indexOf("Java", 5) returns 11.
"Welcome to Java".indexOf("java", 5) returns -1.
"Welcome to Java".lastIndexOf('a') returns 14.

Convert Character and Numbers to Strings

The String class provides several static valueOf methods for converting a character, an array of characters, and numeric values to strings. These methods have the same name valueOf with different argument types char, char[], double, long, int, and float. For example, to convert a double value to a string, use String.valueOf(5.44). The return value is string consists of characters '5', '.', '4', and '4'.

Methods for comparing strings

Method Description
equals(String) Returns a boolean true value if the specified string is equal to the current string. Comparison is case-sensitive.
equalsIgnoreCase(String) Returns a boolean true value if the specified string is equal to the current string.
Comparison is not case-sensitive.
startsWith(String) Returns a boolean true value if this string starts with the specified string.
startsWith(String, startIndex) Returns a boolean true value if this string starts with the specified string starting at the start index.
endsWith(String) Returns a boolean true value if the string ends with the specified string.
isEmpty() Returns a Boolean true value if this string contains an empty string (introduced in Java 1.6).
compareTo(String) Returns an int that's less than zero if the string is less than the specified string, greater than zero if the string is greater than the specified string, and zero if the strings are equal.
compareToIgnoreCase(String) The same as compareTo, but the case of the strings is ignored.

String Comparisons

  • equals
  • 
    	String s1 = new String("Welcome");
    	String s2 = "welcome";
    
      if (s1.equals(s2)){
        // s1 and s2 have the same contents
      }
    
      if (s1 == s2) {
        // s1 and s2 have the same reference
      }
    
  • compareTo(Object object)
  • 	String s1 = new String("Welcome");
    	String s2 = "welcome";
    
      if (s1.compareTo(s2) > 0) {
        // s1 is greater than s2
      }
      else if (s1.compareTo(s2) == 0) {
        // s1 and s2 have the same contents
      }
      else
         // s1 is less than s2
    

String Conversions

The contents of a string cannot be changed once the string is created. But you can convert a string to a new string using the following methods:

  • toLowerCase
  • toUpperCase
  • trim
  • replace(oldChar, newChar)
Previous | Constructors | Create strings | Methods | Code | Next