Strings and Characters: String String
Strings and Characters: String String
Outline
11.1
11.2
11.3
11.4
1.6
Introduction
Fundamentals of Characters and Strings
Class String
11.3.1
String Constructors
11.3.2
String Methods length, charAt and getChars
11.3.3
Comparing Strings
11.3.4
Locating Characters and Substrings in Strings
11.3.5
Extracting Substrings from Strings
11.3.6
Concatenating Strings
11.3.7
Miscellaneous String Methods
11.3.8
String Method valueOf
Class StringBuffer
11.4.1
StringBuffer Constructors
11.4.2
StringBuffer Methods length, capacity,
setLength and ensureCapacity
11.4.3
StringBuffer Methods charAt, setCharAt,
getChars and reverse
11.4.4
StringBuffer append Methods
11.4.5
StringBuffer Insertion and Deletion Methods
Class StringTokenizer
11.1 Introduction
String and character processing
Class java.lang.String
Class java.lang.StringBuffer
Class java.util.StringTokenizer
String
Series of characters treated as single unit
May include letters, digits, etc.
Object of class String
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Outline
String defaultStringConstruct
constructor
ors.java
instantiates empty
string
String constructors
s1 = new String();
s2 = new String( s );
s3 = new String( charArray );
s4 = new String( charArray, 6, 3 );
s5 = new String( byteArray, 4, 4 );
s6 = new String( byteArray );
LineString
17
copies
Linecharacter
18
copies
array
Line 19
Constructor copies
character-array subset
Line 20
Line
21array
Constructor copies
byte
Line 22
Constructor copies byte-array subset
23
24
25
26
27
28
29
30
31
32
33
34
Outline
StringConstruct
ors.java
Method charAt
Get character at specific location in String
Method getChars
Get entire set of characters in String
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Outline
StringMiscellan
eous.java
Line 16
Line 21
Determine number of
characters in String s1
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Outline
Line 25
Method equals
Method equalsIgnoreCase
Method compareTo
Method regionMatches
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Outline
StringCompare.j
ava
Line 18
Line 24
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
)
+
+
+
+
is " + s1.compareTo( s2 ) +
s2.compareTo( s1 ) +
s1.compareTo( s1 ) +
s3.compareTo( s4 ) +
s4.compareTo( s3 ) + "\n\n";
Outline
StringCompare.j
ava
Line 30
Method compareTo
compares String objects
Lines 36-40
Line 43 and 49
Method regionMatches
// test regionMatches (case sensitive)
if ( s3.regionMatches( 0, s4, 0, 5 ) )
compares portions of two
output += "First 5 characters of s3 and s4 match\n";
String objects for equality
else
output += "First 5 characters of s3 and s4 do not match\n";
// test regionMatches (ignore case)
if ( s3.regionMatches( true, 0, s4, 0, 5 ) )
output += "First 5 characters of s3 and s4 match";
else
output += "First 5 characters of s3 and s4 do not match";
53
54
55
56
57
58
59
60
Outline
StringCompare.j
ava
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Outline
StringStartEnd.
java
Line 15
Line 24
Method startsWith
determines if String starts
with specified characters
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Outline
output += "\n";
// test method endsWith
for ( int count = 0; count < strings.length; count++ )
StringStartEnd.
java
Line 33
Method endsWith
String ends
with specified characters
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Outline
StringIndexMeth
ods.java
Lines 12-16
Lines 19-26
Method lastIndexOf
finds last occurrence of
character in String
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Outline
StringIndexMeth
ods.java
Lines 29-46
Methods indexOf and
lastIndexOf can also find
occurrences of substrings
Outline
StringIndexMeth
ods.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Outline
SubString.java
Line 13
Line 16
Beginning at index 20,
extract characters from
String letters
Extract characters from index 3
to 6 from String letters
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Outline
StringConcatena
tion.java
Line 14
Concatenate String s2
Line 15
to String s1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
StringMiscellan
eous2.java
Outline
Line 17
Use method replace to return s1
copy in which every occurrence of
Line 20
l is replaced with L
Line 21 to
toUpperCase
return s1 copy in which every
Line 24
character is uppercase
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Outline
StringMiscellan
eous2.java
Line 27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Outline
StringValueOf.j
ava
Lines 20-26
27
28
29
30
31
32
33
34
Outline
StringValueOf.j
ava
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Outline
toString returns
String representation of
StringBuffer
Method length
Return StringBuffer length
Method capacity
Return StringBuffer capacity
Method setLength
Increase or decrease StringBuffer length
Method ensureCapacity
Set StringBuffer capacity
Guarantee that StringBuffer has minimum capacity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
StringBufferCap
Len.java
Outline
Method length
Line 12 returns
StringBuffer length
Line 12
Method capacity returns
StringBuffer
Line 14 capacity
Line 17
Use method ensureCapacity
to set capacity to 75
Use method setLength
to set length to 10
25
26
27
28
System.exit( 0 );
}
} // end class StringBufferCapLen
Outline
StringBufferCap
Len.java
Only 10 characters
from
StringBuffer are
printed
Method setCharAt
Set StringBuffer character at specified index
Method getChars
Return character array from StringBuffer
Method reverse
Reverse StringBuffer contents
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Outline
StringBufferCha
rs.java
Lines 12-13
Return StringBuffer
characters at indices 0
Line 16
and 4, respectively
Lines 22-23
Return character array
from StringBuffer
Replace characters at
indices 0 and 6 with H
and T, respectively
26
27
28
29
30
31
32
33
34
35
36
buffer.reverse();
output += "\n\nbuf = " + buffer.toString();
JOptionPane.showMessageDialog( null, output,
"StringBuffer character methods",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
} // end class StringBufferChars
Outline
Reverse characters in
StringBuffer
StringBufferCha
rs.java
Lines 26
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Outline
StringBufferApp
end.java
Line 21
Line 23
Line 25
Line 27
objectRef );
to StringBuffer
" " );
// each of these contains two spaces
string );
Append String goodbye
" " );
charArray );
Append a b c d e f
" " );
charArray, 0, 3 );
Append a b c
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
buffer.append(
buffer.append(
buffer.append(
buffer.append(
buffer.append(
buffer.append(
buffer.append(
buffer.append(
buffer.append(
buffer.append(
buffer.append(
buffer.append(
buffer.append(
buffer.append(
" " );
booleanValue );
" " );
characterValue );
" " );
integerValue );
" " );
longValue );
" " );
floatValue );
" " );
doubleValue );
" " );
lastBuffer );
Outline
StringBufferApp
Append boolean, char,
int,
end.java
long, float and double
Line 29-39
JOptionPane.showMessageDialog( null,
"buffer = " + buffer.toString(), "StringBuffer append Methods",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
} // end StringBufferAppend
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
0,
0,
0,
0,
0,
0,
0,
objectRef );
" " ); // each of these contains two spaces
string );
" " );
Use method insert to insert
charArray );
data in beginning of
" " );
StringBuffer
charArray, 3, 3 );
Outline
StringBufferIns
ert.java
Lines 20-26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
buffer.insert(
buffer.insert(
buffer.insert(
buffer.insert(
buffer.insert(
buffer.insert(
buffer.insert(
buffer.insert(
buffer.insert(
buffer.insert(
buffer.insert(
buffer.insert(
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
Outline
" " );
booleanValue );
" " );
characterValue );
" " );
integerValue );
" " );
longValue );
" " );
floatValue );
" " );
doubleValue );
// delete 5 in 2.5
// delete .333 in 33.333
Outline
StringBufferIns
ert.java