Chapter 2 & 9
Chapter 2 & 9
position = str.indexOf("and");
while (position != -1)
{
System.out.println(position);
position = str.indexOf("and", position + 1);
}
String Methods For Getting Character Or Substring Location
String Methods For Getting Character Or Substring Location
Extracting Substrings
• The String class provides methods to extract
substrings in a String object.
– The substring method returns a substring beginning
at a start location and an optional ending location.
Address “Smith”
Extracting Substrings
• StringBuilder(int length)
– This constructor gives the object enough storage space to hold
length characters.
• StringBuilder(String str)
– This constructor initializes the object with the string in str.
– The object will have at least enough storage space to hold the string
in str.
Other StringBuilder Methods
System.out.println(str);
int i = 12;
double d = 14.95;
String str1 = Integer.toString(i);
String str2 = Double.toString(d);
The toBinaryString, toHexString, and
toOctalString Methods
ArrayList<int> list =
new ArrayList<int>(); // Error!
ArrayList<Integer> list =
new ArrayList<Integer>(); // OK!