The String Class




A String is a class (i.e. an object when created at runtime). However, you do not have to use new (although you can) when you create a String object. There is a difference between String s = "abc"; and String s = new String("abc"); However, we will deal with that difference later. A String is an object that at runtime holds a sequence of characters and is tied to many methods.

Example:

Instance variables (available to all methods)
  
   private String firstName;
   private String lastName;
   public String s = "Hello World";



Note that a String object is immutable!

An object can be joined with a String (also an object) to produce a new String. Also, primitives can be joined with a String (also an object) to produce a new String. (remember primitive types are double, float, long, int, short, char, byte, boolean, ...) Evaluation is the same as in Algebra.
Examples:

"abc" + "def" --> "abcdef"

7 + "abc" --> "7abc"

7 + 6 + "abc" --> "13abc" (since 7+6 is done first)

7 + "abc" + 6 --> "7abc6"

7 + "abc" + 6*3 --> "7abc18" (since 6*3 is done first)

7 * "abc" --> illegal, you cannot multiply with a String


Some Common Methods:

toString() returns a String which is the same as the original String
length() returns the number of characters in the String (int)
charAt() returns the character (char) at the given position (index)
indexOf() searches for a given character or String and returns the position found or -1 if not found
equals() compares this String to a given String and returns true or false
compareTo() compares this String to a given String and returns a negative, 0, or positive int (dictionary order)
substring() returns a new String which is a portion of the original String
replace() returns a String replacing a given char with another given char
replace() returns a String replacing a given String with another given String
replaceAll() returns a String replacing a given String with another given String


String s1 = "abc de";
String s2 = "abc cde";

out.println(s1); // prints out abc de
out.println(s1.toString()); // prints out abc de
out.println(s2); // prints out abc cde
out.println(s2.toString()); // prints out abc cde
out.println("abc"); // prints out abc
out.println();

out.println(s1.length()); // prints out 6
out.println(s2.length()); // prints out 7
out.println("abc".length()); // prints out 3
out.println();

out.println(s1.charAt(0)); // prints out the character a
out.println(s1.charAt(2)); // prints out the character c
out.println();

out.println(s1.indexOf('c')); // prints out 2
out.println(s1.indexOf("bc")); // prints out 1
out.println(s1.indexOf("ac")); // prints out -1
out.println();

out.println(s1.equals(s2)); // prints out false
out.println(s1.equals("abc de")); // prints out true
out.println();

out.println(s1.compareTo(s2)); // prints out 1 ('d' - 'c')
out.println(s1.compareTo("abc de")); // prints out 0
out.println();

// the first parameter is the starting position
// the second parameter (if present) is the stopping position (non inclusive)
out.println(s1.substring(4,6)); // prints out de
out.println(s1.substring(0,3)); // prints out abc
out.println(s1.substring(2)); // prints out c de
out.println("abcabc".substring(3)); // prints out abc
out.println(s2.substring(2)); // prints out c cde
out.println();

// the first parameter is replaced by the second parameter
out.println(s1.replace('c','a')); // prints out aba de
out.println(s1.replace("c","a")); // prints out aba de

// the first parameter is a regular expression
out.println(s1.replaceAll("c","a")); // prints out aba de
out.println(s1.replaceAll("ab","f")); // prints out fc de
out.println("abcabca".replaceAll("ab","f")); // prints out fcfca


Other Useful Methods:

contains() returns true if this String contains the parameter String else false
isEmpty() returns true if this String is empty ("")(length()==0) else false
toLowerCase() returns a new String which has all the letters in lower case
toUpperCase() returns a new String which has all the letters capitalized
trim() returns a new String which has all leading and trailing white space characters removed
valueOf() returns a new String which contains the input primitive parameter (overloaded)
split() returns an array of String objects (String []) by parsing the input parameter String
format() returns a formatted String


String s3 = "Hello 5";
String s4 = "Bye";

out.println(s3.contains("lo")); // prints true
out.println(s3.isEmpty()); // prints false
out.println(s3.toLowerCase()); // prints hello 5
out.println(s3.toUpperCase()); // prints HELLO 5

s3 = " Hello ";
out.println(s3.trim()); // prints Hello (no pre or post spaces, tabs, or new lines)

out.println(String.valueOf(32)); // prints 32 (returns "32" i.e. as a String)
out.println(String.valueOf(32.0)); // prints 32.0 (returns "32.0" i.e. as a String)
out.println(String.format("The area is %6.1f square units.",24.38745)); // prints The area is 24.4 square units.
  • format help

  • format examples