1.
Constructing a String
If you are constructing a string with several appends, it may be more efficient to construct it using a
StringBuffer and then convert it to an immutable String object.
StringBuffer buf = new StringBuffer("Initial Text");
// Modify
int index = 1;
buf.insert(index, "abc");
buf.append("def");
// Convert to string
String s = buf.toString();
Getting a Substring from a String
int start = 1;
int end = 4;
String substr = "aString".substring(start, end); // Str
2.
String string = "aString";
// First occurrence.
int index = string.indexOf('S'); // 1
// Last occurrence.
index = string.lastIndexOf('i'); // 4
// Not found.
index = string.lastIndexOf('z'); // -1