Java Reference
In-Depth Information
Display 1.7
Using the String Class
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
You could just use 4 here, but if you
had a String variable instead of
"hate", you would have to use length
as shown.
01234567890123456789012
I hate text processing!
The word "hate" starts at index 2
The changed string is:
I adore text processing!
ASCII character set plus many of the characters used in languages with a different
alphabet 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 Greek-language keyboard and
monitor to do this). The disadvantage of the Unicode character set is that it sometimes
requires more computer memory to store each character than it would if Java used only
the ASCII character set.
 
 
Search WWH ::




Custom Search