Java Reference
In-Depth Information
Statement
Effect / Explanation
str2 = sentence.substring(2, 12); str2 = "w is the t"
In sentence , the substring starting at index
2 until the index 11 (= 12 - 1) is "w is the
t" . So the value assigned to str2 is "w is
the t" .
index = 24
str1 = "birthday party"
The starting index of "birthday" in
sentence is 24. So the value of index is 24.
Now index is 24, so index + 14 is 38. The
substring starting at the position 24 until the
position 37 (= 38 - 1) is "birthday
party" .
index =
sentence.indexOf("birthday");
str1 = sentence.substring(index,
index + 14);
sentence.replace('t', 'T')
Returns:
"Now is The Time for The
birThday parTy"
sentence.toUpperCase()
Returns:
"NOW IS THE TIME FOR THE
BIRTHDAY PARTY"
The following program tests the preceding statements:
// This program illustrate how various String methods work.
public class VariousStringMethods
{
public static void main(String[] args)
{
String sentence;
String str1;
String str2;
String str3;
int index;
sentence = "Now is the time for the birthday party";
System.out.println("sentence = \"" + sentence + "\"");
System.out.println("The length of sentence = "
+ sentence.length());
System.out.println("The character at index 16 in "
+ "sentence = " + sentence.charAt(16));
System.out.println("The index of first t in sentence = "
+ sentence.indexOf('t'));
System.out.println("The index of for in sentence = "
+ sentence.indexOf("for"));
 
Search WWH ::




Custom Search