We will generally follow the presentation in Ch. 7 of the textbook, which gives further details and examples. Also, for even more information, you can see the official Java documentation for String and StringBuffer.
Our basic outline of coverage is as follows:
class
It is indeed a class, not a primitive data type (though
admittedly, it is given some special treatment, not
available to user defined classes)
instantiation
There are several different ways to instantiate a String
object; interestingly, once it is created it cannot be
changed (i.e., it is immutable).
Possible ways to create a string include:
String literals are automatically instantiated, e.g.,
String school = "Saint Louis University";
Using a Constructor. There are several forms, such as
String() -- creates a string of length zero (i.e., "").
String boring = new String();
String school = "Saint Louis University";
String home = new String(school);
Through the use of the concatenation
operator,
String school = "Saint Louis University";
String mascot = school + " Billiken";
Though a String object cannot be changed, a String reference variable may certainly be changed.
Furthermore, the reference variable can be (re)assigned based on the return value of a method declared with a String reference as a return type.String school;
school = "Notre Dame";
school = "Saint Louis University";
String school = gameWinner();
indexing string
The position of a particular character in a given string
is called its index. Because of the way numbers
are represented in hardware, it is customary that indices
begin with 0. For example, the five characters in the
string "Hello" will be indexed as
supported methods
int length() -- returns the number of characters in the given string.
char charAt(int index) -- returns the character at the specified index.
String substring(int start, int end) -- returns a reference to a newly constructed string which is equivalent to the substring of the given string, begining at index start, yet ending immediately before index end (i.e., end is not inclusive)
int indexOf(String pattern) -- searches for the leftmost occurrence of the given pattern in the string. Returns the starting index of that occurrence, or -1 if no such occurrences were found.
int indexOf(String pattern, int startingIndex) -- searches for the leftmost occurrence of the given pattern in the string, however starting on or after the startingIndex.
int lastIndexOf(String pattern)
int lastIndexOf(String pattern, int startingIndex)
similar to indexOf, however tries to find the
rightmost occurrence of the pattern in the given range.
data conversion
Two separate issues are how other data types might be converted to a string, as well as how a string might be converted to another data type. We will discuss these issues separately.
Every data type in Java can be converted to a string. For primitive data types, you can do so explicitly through the static method String.valueOf, such as
However, because concatenation is so commonly needed in such settings, Java will automatically convert any non-string data to a String, if used within a larger string concatenation. For example, the following is allowable:double data;
String s = String.valueOf(data); // data could be any type
int age = 35;
String greeting = "My age is "+value(age); // using String concatenation
int age = 35;
String greeting = "My age is "+age; // note the mix of a string and an int
It is also possible to convert a given String to many other data types, such as numeric primitive. For example, converting the character string "-35.368" to a double. This is known as parsing the string, however it is not as straight forward. How to interpret the string is specific to each data type, and so a specific method must exist for each such conversion. Furthermore, those methods must decide how to respond when asked to parse a string which is not of the expected format, for example if asked to convert "-345.43.57.X" to a double.
equivalence of strings
Recall the important lesson regarding the difference between a reference variable and the actual object which it reference. How might you think the following behaves?
The conditional test fails, as it happens. Though both person and leader reference strings which are comprised of the characters "Alice", they are not the same object. They are represented in two different places in memory; the reference variables are not the same.String person = "Alice";
String leader = new String(person);
if (person==leader) {
System.out.prinln("Success");
}
Of course, we may at times wish to test for the
equivalence of two strings, in respect to the sequence of
characters they represent, even if the two strings are not
truly the same object. For this reason, a String object
supports an additional method declared as
and used as:
In fact, other forms of equivalence can be defined, such asString person = "Alice";
String leader = new String(person);
if (person.equals(leader)) { // this test will succeed
System.out.prinln("Success");
}
The template for our starting point is available on patel2 at /home/csa120/labs/palindrome
conversion between String and StringBuffer
Converting between class String and StringBuffer is quite easy.
Given a String, you can construct a new StringBuffer with the
same character sequence, using the constructor
Similarly, if you have a StringBuffer object, and you want to
construct an equivalent String, you can use the method
accessor methods common to both String and StringBuffer
mutator methods supported only by StringBuffer
sets the character at the specified index to ch.
appends a copy of
the string s to the end of the current StringBuffer.
inserts a copy of the string s into the StringBuffer
placed so that that sequence of characters will begin at index
offset (while shifting the remainder of the
original buffer so that it follows the newly inserted string)
deletes the substring begining at index
start, yet ending immediately before index end (i.e., end is not inclusive)