Joining Strings
Example – I (String
Concatenation)
JoinStrings.java
TestConcat.jav
a
The conversion of values of the basic
types to type String is actually
accomplished by using a static method,
toString(), of a standard class that
corresponds to the basic type (wrapper
class).
Basic The
Typeclasses
Wrapper Class
in the table are called wrapper classes
bytebecause objectsByteof each of these class types “ wrap” a value
shortof the corresponding
Short primitive type. Whenever a value of
one of the basic types appears as an operand to + and the
intother operandInteger
is a String object, the compiler arranges to
longpass the value Longof the basic type as the argument to the
float Float that is defined in the corresponding
toString() method
doublewrapper class.
DoubleThe toString() method returns the String
equivalent ofBoolean
boolean the value. All of this happens automatically
when you are concatenating strings using the + operator.
char Character
String Interning
This feature makes it possible to compare
strings with the == operator effective.
String interning ensures that no 2 String
objects encapsulate the same string, so all
String objects encapsulate unique strings.
This means that if 2 String variables
reference strings that are identical, the
references must be identical, too.
To arrange that all String objects
encapsulate unique strings, use intern().
Sequencing
Compares 2 strings by Strings
comparing successive corresponding
characters, starting with the 1st character in each string. The
process continues until either corresponding characters are
string1.compareTo(string2)
found to be different, or the last character in one or both
strings is reached. Characters are compared by comparing
ValueUnicode representations—so
their Meaning 2 characters are equal if
< 0numeric
the Invoking
valuesstring < strUnicode representations are equal.
of their
> 0 character
One Invokingis string
greater>str
than another if the numerical value of
its 0Unicode
The 2representation
strings are equalis greater than that of the other. A
character is less than another if its Unicode code is less than
that of the other.
int compareToIgnoreCase(StringCase
str) differences ignored
Example – IV (Ordering
Strings)
SequenceStrings.java
SortString.java
Extracting String
int aIndex = -1; // Position of 1st ‘a’ Finds the 1st ‘b’ that
int bIndex = -1; // Position of 1st ‘b’ comes
after ‘a’after the 1st ‘a’ in a
aIndex = text.indexOf(‘a’); // Find first ‘a’
string
if (aIndex >= 0) { // Make sure you found ‘a’
bIndex = text.indexOf(‘b’, ++aIndex); // Find 1st ‘b’
after 1st ‘a’
} int aIndex = -1; // Search start position
int count = 0; // Count of ‘a’ Used to count how many
occurrences
times a particular character
while ((aIndex = text.indexOf(‘a’, ++aIndex))
occurred in a string.
> -1) {
++count;
}
Searching for
Method Description
indexOf(int ch) Returns the index
position of the 1st
occurrence of the
character ch in the String
for which the method is
called. If the character ch
does not occur, -1 is
returned.
indexOf(int ch, Same as the preceding
int index) method, but with the
search starting at position
index in the string. If the
value of index is outside
the legal limits for the
String object, -1 is
returned.
indexOf(String Returns the index
str) position of the 1st
occurrence of the
substring str in the String
object for which the
method is called. If the
substring str does not
occur, -1 is returned.
indexOf(String Same as the preceding
str, int index) method, but with the
search starting at position
index in the string. If the
value of index is outside
the legal limits for the
String object, -1 is
returned.
String string1 = “The Ides of March”;
System.out.println(string1.startsWith(“Ides”, 4));
Returns true
Enables
String segment = place.substring(7, 11); fromsta string by
us to extract a substring
specifying the index positions of the 1 character
in the substring and one beyond the last
character of the substring as arguments to the
method.
The variable segment is set to the string “ring”:
Example – VII (Word for
Word)
ExtractSubstrings.jav
a
Tokenizing a
String
Splits a string into tokens.
split()
Does this in a single step, returning all the tokens from a
; string as an array of String objects.
Appending Basic
Types
StringBuffer buf = new StringBuffer(“The number is “);
long number = 999;
buf.append(number); // buf contains ‘The number is 999’
char[ ] text = { ‘i’, ‘s’, ‘ ‘, ‘e’, ‘x’, ‘a’, ‘c’, ‘t’, ‘l’, ‘y’};
buf.append(text, 2, 8); // buf contains “The number is 99912.34
exactly”.
Data types that can be appended to a
StringBuffer object
boolean int long String Object
char byte short float double
Inserting
Strings
// Assume buf contains the string “Many hands make light work”,
buf.insert(4, “ old”); // buf will contain the string “Many old hands
make light work” nd
Data types that can be used in insert as a 2
argument
boolean int long String Object
char byte short float double
Inserts a substring into the StringBuffer
insert(int index, char[
object] str , int offset
starting , int length
at position ) The
index.
substring is the String representation of
length characters from the str[ ] array,
starting at position offset.
UseStringBuffer.java