Java String Class
Java String Class
IT201
1
String CLASS
2
The String class
• An object of the String class represents a
string of characters.
• The String class belongs to the java.lang
package, which is built into Java.
• Like other classes, String has constructors
and methods.
• Unlike other classes, String has two
operators, + and += (used for concatenation).
Amity University
Literal Strings
Amity University
Immutability
• Once created, a string cannot be changed:
none of its methods changes the string.
• Such types of objects are called immutable.
• Immutable objects are convenient because
two references can point to the same object
safely.
• There is no danger of changing an object
through one reference without the others
being aware of the change.
Amity University
String s1 = "Sun"; String s1 = "Sun";
String s2 = s1; String s2 = new String(s1);
• Advantage: more efficient, no need to copy.
s1 s1 "Sun"
"Sun"
s2 s2 "Sun"
OK Less efficient
and wasteful
Amity University
• Disadvantage:
String s = "sun";less efficient — you need to
create
char cha =new string and throw away
Character.toUpper(s.charAt the old
(0));
s = for
one ch every
+ s.substring
small (1);
change.
s "sun"
'S' + "un"
Amity University
Empty Strings
• An empty string has no characters; its
length is 0.
String s1 = ""; Empty strings
String s2 = new String();
Amity University
Constructors
Amity University
Methods
int length ();
— length, charAt
• Returns the number of
characters in the
char charAt string
(k); • Returns the k-th char
Amity University
Methods — substring
String s2 = s.substring (i, k); strawberry
– returns the substring of chars in i k
positions from i to k-1
result += s3;
– concatenates s3 to result
result += num;
– converts num to String and concatenates
it to result
Amity University
Methods — Find (indexOf)
0 8 11 15
Amity University
Methods — Replacements
String s2 = s1.trim ();
– returns a new string formed from s1 by
removing white space at both ends
String s2 = s1.replace(oldCh, newCh);
– returns a new string formed from s1 by
replacing all occurrences of oldCh with newCh
String s2 = s1.toUpperCase();
String s2 = s1.toLowerCase();
– returns a new string formed from s1 by
converting its characters to upper (lower) case
Amity University
Replacements (cont’d)
Amity University
Numbers to Strings and
Strings to Numbers
Amity University
Numbers to Strings
• Three ways to convert a number into a string:
1.
String s = "" + num;
2.
String s = Integer.toString (i); int i;
String s = Double.toString (d); double d;
3.
String s = String.valueOf (num);
Amity University
Numbers to Strings (cont’d)
• The DecimalFormat class can be used
for more controlled conversions of
numbers into strings:
import java.text.DecimalFormat;
...
DecimalFormat money =
new DecimalFormat("0.00"); 56.7899
...
double amt = …;
"56.79"
...
String s = money.format (amt);
Amity University
Character Methods
• java.lang.Character is a class that
represents characters as objects.
• Character has several useful static
methods that determine the type of a
character.
• Character also has methods that convert
a letter to the upper or lower case.
Amity University
Character Methods (cont’d)
if (Character.isDigit (ch)) ...
.isLetter...
.isLetterOrDigit...
.isUpperCase...
.isLowerCase... Whitespace is
.isWhitespace... space, tab,
– return true if ch belongs to thenewline, etc.
corresponding category
Amity University
StringTokenizer
• java.util.StringTokenizer is used to extract
“tokens” from a string.
• Tokens are separated by delimiters (e.g.,
whitespace).
• A tokenizer object is constructed with a given
string as an argument.
• The second optional argument is a string that
lists all delimiters (default is whitespace).
Amity University
StringTokenizer (cont’d)
import java.util.StringTokenizer; Delimiters are
... whitespace
String str = input.readLine();
StringTokenizer q =
new StringTokenizer (str); All delimiters
// or:
// new StringTokenizer (str, ";+ \t, ");
The number of
int n = q.countTokens ();
found tokens
while ( q.hasMoreTokens() )
{
String word = q.nextToken();
...
Amity University
String Buffer Class
Amity University