Java Reference
In-Depth Information
Display 1.7
Using the
String
Class
You could just use
4
here, but if you
had a
String
variable instead of
"hate"
, you would have to use
length
as shown.
1
public class
StringProcessingDemo
2 {
3
public static void
main(String[] args)
4 {
5 String sentence = "I hate text processing!";
6
int
position = sentence.indexOf("hate");
7 String ending =
8 sentence.substring(position + "hate".length());
9
10 System.out.println("01234567890123456789012");
11 System.out.println(sentence);
12 System.out.println("The word \"hate\" starts at index "
13
+ position);
14 sentence = sentence.substring(0, position) + "adore"
15 + ending;
16 System.out.println("The changed string is:");
17 System.out.println(sentence);
18 }
19 }
Sample Dialogue
01234567890123456789012
I hate text processing!
The word "hate" starts at index 2
The changed string is:
I adore text processing!
the corresponding number. Java, and now many other programming languages,
uses the
Unicode
character set. The
Unicode
character set includes the ASCII
character set plus many of the characters used in languages with a different alpha-
bet from English. This is not likely to be a big issue if you are using an English-
language keyboard. Normally, you can just program as if Java were using the
ASCII character set. The ASCII character set is a subset of the Unicode character
set, and the subset you are likely to use. Thus, Appendix 3, which lists the ASCII
character set, in fact lists the subset of the Unicode character set that we will use in
this topic. The advantage of the Unicode character set is that it makes it possible
to easily handle languages other than English. For example, it is legal to spell a
Java identifier using the letters of the Greek alphabet (although you may want a
Unicode