Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
I/O
Lecture 4
Prepared by Muhanad
Alkhalisy
1
Motivations
Often you encounter the problems that involve string
processing and file input and output. Suppose you need to
write a program to replace all occurrences of a word with a
new word in a file. How do you solve this problem? This
chapter introduces strings and text files, which will enable
you to solve this problem.
String in Java
A string is a sequence of characters. In many languages,
strings are treated as an array of characters, but in Java a
string is an object. The String class has 11 constructors and
more than 40 methods for manipulating strings. Not only is
it very useful in programming, but also it is a good
example for learning classes and objects.
Constructing a String:
String message = "Welcome to Java;
String message = new String("Welcome to Java);
Constructing Strings
String newString = new String(stringLiteral);
String message = new String("Welcome to Java");
Since strings are used frequently, Java provides a
shorthand initializer for creating a string:
String message = "Welcome to Java";
5
animation
Trace Code
String s = "Java";
s = "HTML";
: String
String object for "Java"
: String
String object for "Java"
: String
String object for "HTML"
animation
Trace Code
String s = "Java";
s = "HTML";
After executing s = "HTML";
: String
String object for "Java"
: String
String object for "Java"
: String
String object for "HTML"
Interned Strings
Since strings are immutable and are frequently
used, to improve efficiency and save memory, the
JVM uses a unique instance for string literals with
the same character sequence. Such an instance is
called interned. For example, the following
statements:
Examples
String s1 = "Welcome to Java";
String s2 = new String("Welcome to Java");
s1
s3
: String
Interned string object for
"Welcome to Java"
display
s1 == s is false
s1 == s3 is true
: String
A string object for
"Welcome to Java"
animation
Trace Code
Strings1="WelcometoJava";
Strings2=newString("WelcometoJava");
Strings3="WelcometoJava";
s1
: String
Interned string object for
"Welcome to Java"
11
Trace Code
Strings1="WelcometoJava";
Strings2=newString("WelcometoJava");
Strings3="WelcometoJava";
s1
: String
Interned string object for
"Welcome to Java"
s2
: String
A string object for
"Welcome to Java"
12
Trace Code
Strings1="WelcometoJava";
Strings2=newString("WelcometoJava");
Strings3="WelcometoJava";
s1
s3
: String
Interned string object for
"Welcome to Java"
s2
: String
A string object for
"Welcome to Java"
13
Output : hello
14
String Comparisons
java.lang.String
+equals(s1: String): boolean
+equalsIgnoreCase(s1: String):
boolean
+compareToIgnoreCase(s1: String):
int
+regionMatches(toffset: int, s1: String, Returns true if the specified subregion of this string exactly
offset: int, len: int): boolean
matches the specified subregion in string s1.
+regionMatches(ignoreCase: boolean, Same as the preceding method except that you can specify
toffset: int, s1: String, offset: int,
whether the match is case-sensitive.
len: int): boolean
+startsWith(prefix: String): boolean
15
String Comparisons
equals
String s1 = new String("Welcome);
String s2 = "welcome";
if (s1.equals(s2)){
// s1 and s2 have the same contents
}
if (s1 == s2) {
// s1 and s2 have the same reference
}
16
compareTo(Object object)
String s1 = new String("Welcome);
String s2 = "welcome";
if (s1.compareTo(s2) > 0) {
// s1 is greater than s2
}
else if (s1.compareTo(s2) == 0) {
// s1 and s2 have the same contents
}
else
// s1 is less than s2
17
String Equal
String Compare
Returns a new string that concatenate this string with string s1.
string.
20
21
String Length
Output : 17
22
Use message.charAt(index)
Indices
message
message.charAt(0)
message.length() is 15
10 11 12 13 14
J
message.charAt(14)
23
String Index Of
Output : f
24
String Concatenation
String s3 = s1.concat(s2);
String s3 = s1 + s2;
s1 + s2 + s3 + s4 + s5 same as
(((s1.concat(s2)).concat(s3)).concat(s4)).concat(s5);
25
String Index Of
Extracting Substrings
java.lang.String
+subString(beginIndex: int): Returns this strings substring that begins with the character at the
specified beginIndex and extends to the end of the string,
String
Returns this strings substring that begins at the specified
+subString(beginIndex: int,
beginIndex and extends to the character at index endIndex 1,
endIndex: int): String
Note that the character at endIndex is not part of the substring.
27
Extracting Substrings
You can extract a single character from a string using the
charAt method. You can also extract a substring from a
string using the substring method in the String class.
String s1 = "Welcome to Java";
String s2 = s1.substring(0, 11) + "HTML";
Indices
message
message.substring(0, 11)
10 11 12 13 14
J
message.substring(11)
28
String Index Of
Output : ell
29