img
sb.replace(5, 7, "was");
System.out.println("After replace: " + sb);
}
}
Here is the output:
After replace: This was a test.
substring( )
You can obtain a portion of a StringBuffer by calling substring( ). It has the following two
forms:
String substring(int startIndex)
String substring(int startIndex, int endIndex)
The first form returns the substring that starts at startIndex and runs to the end of the
invoking StringBuffer object. The second form returns the substring that starts at startIndex
and runs through endIndex­. These methods work just like those defined for String that
1
were described earlier.
Additional StringBuffer Methods
In addition to those methods just described, StringBuffer includes several others. They are
summarized in the following table. Notice that several were added by J2SE 5.
Method
Description
StringBuf fer appendCodePoint(int ch)
Appends a Unicode code point to the end of the invoking object.
A reference to the object is returned. Added by J2SE 5.
int codePointAt(int i)
Returns the Unicode code point at the location specified by i.
Added by J2SE 5.
int codePointBefore(int i)
Returns the Unicode code point at the location that precedes
that specified by i. Added by J2SE 5.
int codePointCount(int star t, int end)
Returns the number of code points in the por tion of the invoking
String that are between star t and end­1. Added by J2SE 5.
int indexOf(String str)
Searches the invoking StringBuffer for the first occurrence of str.
Returns the index of the match, or ­1 if no match is found.
int indexOf(String str, int star tIndex)
Searches the invoking StringBuffer for the first occurrence of str,
beginning at star tIndex. Returns the index of the match, or ­1
if no match is found.
int lastIndexOf(String str)
Searches the invoking StringBuffer for the last occurrence of str.
Returns the index of the match, or ­1 if no match is found.
int lastIndexOf(String str, int star tIndex)
Searches the invoking StringBuffer for the last occurrence of str,
beginning at star tIndex. Returns the index of the match, or ­1
if no match is found.
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home