Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Two of the most commonly used classes in the Java API are String and StringBuffer (remember from #9 a few pages back, Strings are immutable, so a StringBuffer/StringBuilder can be a lot more efficient if you’re manipulating a String). As of Java 5.0 you should use the StringBuilder class instead of StringBuffer, unless your String manipulations need to be thread-safe, which is not common. Here’s a brief overview of the key methods in these classes:
Both String and StringBuffer/StringBuilder classes have:
|
char charAt(int index); |
// what char is at a certain position |
|
int length(); |
// how long is this |
|
String substring(int start, int end); |
// get a part of this |
|
String toString(); |
// what’s the String value of this |