String Class:
The String class is a class that has been created to deal with the primitive type- characters. The String class “strings together” characters to form words and sentences (with numbers and symbols possible).
Creating Strings:
The String class has two constructors:
String() – default and String(String s) – copy
String s = new String(); //creates an empty String “”
String t = new String(s); // copies a String as a parameter to form a String
*In this case s1 and s2 are empty Strings.
These constructors are rarely used because……
An easier way to create Strings:
Strings have a unique characteristic to where they can be created as a “String literal”. In this way, a String is created by:
String s = “hello world”;
Concatenation:
A very important feature of Strings is that we can combine two strings together with the plus sign (+).
Example:
String s = “sample”;
String t = “response”;
System.out.println(s + t); // will print sampleresponse (no space)
Null String:
Strings are object (since they come from the String class) so a String that is not initialized is set to null.
String s;
****This is not the same as an empty String. If you try to run a method on a null String you will get a null-pointer exception.
Methods (Very important):
The String class has several methods that you must know how to use on the AP exam.
int length() //finds the length of a string aka how many characters it has
int indexOf(String str) //returns the first index of str (-1 if str is not in the String)
String substring(int start) // returns the substring from the start-th character to the end
String substring(int start, int end) // returns the substring of chars from start to end -1
boolean equals(String other) // returns true if the String calling the method has the
same contents as other
int compareTo(String other) //returns the lexicographical difference
(one calling – other)
Other useful String methods (not required for AP exam):
char charAt(int i) //returns the character at the i-th index
boolean equalsIgnoreCase(String other)
int compareToIgnoreCase(String other)
**Last two methods work as their counterparts but ignores case
Methods in other classes useful with Strings:
Converting Strings to numbers:
Both of the following methods are static methods from the Integer class. The Integer class is a wrapper class that “wraps” ints into Integer objects. The fact that they are static means we call them from their class (similar to how we do Math.sqrt()).
Integer valueOf(String num) //returns an Integer object that is equal to num
int parseInt(String num) // returns an int that is equal to num
Example:
String s = “5”;
int t = Integer.valueOf(s); // now t is an int with value 5
Converting numbers to Strings:
The easiest way to do it is to concatenate the number with an empty String.
Example:
int s = 5;
String t = s + “”; // now t is a String with value “5”
Immutability:
Strings are immutable, which means that once they are created they can’t be changed. Notice that all the methods in the String class returned either ints, chars, or Strings but does not change the Strings themselves.
The closest we can get is:
String s = “Hello World”;
s = s.substring(0,2);
However, this is really creating another String (“He”) and then assigning it back to s.
The benefit of Strings being immutable is that several references can refer to the same String and there is no risk of some of the references being changed on accident.
Escape Characters:
\t
Insert a tab in the text at this point.
\n
Insert a newline in the text at this point.
\'
Insert a single quote character in the text at this point.
\"
Insert a double quote character in the text at this point.
\\
Insert a backslash character in the text at this point.
These have to be placed inside a String to accomplish the task needed.
Example:
String s = “He said \”Hey guys\”, how’s it going?”;
Stores: He said “Hey guys”, how’s it going?
System.out.println( “How is it \ngoing?”);
Prints:
How is it
going