length() returns the length of a String. Notice that length() is a method
while array.length is a member of an array. char charAt(int index) returns the character at the specified index.String substring(int beginIndex), String substring(int beginIndex, int endIndex)
return a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at
index endIndex - 1. char[] toCharArray() converts this string to a new character array.
public class StringTest1 {
public static void main(String[] args) {
String s = "This is a test.";
System.out.println(s.length());
System.out.println(s.charAt(3));
System.out.println(s.substring(3));
System.out.println(s.substring(3,7));
char[] charray = s.toCharArray();
System.out.println(charray[5]);
}
}
|
15 s s is a test. s is i |
The changing of the guard began in earnest Thursday with Arnold Schwarzenegger introducing a team led by a veteran political insider that will shepherd the actor's transition to governor as soon as this week's recall election is certified.
public class StringTest2 {
public static void main(String[] args) {
// break the line in few strings
String s = "The changing of the guard began in earnest Thursday with " +
"Arnold Schwarzenegger introducing a team led by a veteran " +
"political insider that will shepherd the actor's transition to governor " +
"as soon as this week's recall election is certified.";
int count = 0;
for(int i = 0; i < s.length(); i++) {
if(s.charAt(i) == 'a') {
count++;
}
}
System.out.println("There are " + count + "a.");
}
}
|
| There are 17a. |
indexOf(int ch),indexOf(int ch, int fromIndex),lastIndexOf(int ch), lastIndexOf(int ch, int fromIndex),
indexOf(String str), indexOf(String str, int fromIndex),indexOf(String str, int fromIndex),lastIndexOf(String str, int fromIndex):
return the index of the first occurrence (or last occurrence) of the character( or string) or -1 if the character (string) doesn't occur.
public class StringTest3 {
public static void main(String[] args) {
String s = "C++ is a good computer language. Java is the best computer language!";
System.out.println(s.indexOf('a'));
System.out.println(s.indexOf('a', 20));
System.out.println(s.lastIndexOf('u'));
System.out.println(s.lastIndexOf('o', 20));
System.out.println(s.indexOf("computer"));
System.out.println(s.lastIndexOf("computer"));
System.out.println(s.lastIndexOf("abcdefg"));
}
}
|
7 24 63 15 14 50 -1 |
String[] names = {"George Washington", "Abraham Lincoln", "Bill Clinton", "Ronald Reagan","Thomas Jefferson", "George Bush"};
public class LastName {
public static void main(String[] args) {
String[] names = {"George Washington", "Abraham Lincoln", "Bill Clinton",
"Ronald Reagan","Thomas Jefferson", "George Bush"};
for(int i =0; i < names.length; i++) {
System.out.println(names[i].substring(names[i].lastIndexOf(" ")+1));
}
}
}
|
Washington Lincoln Clinton Reagan Jefferson Bush |
equals(String anotherString), equalsIgnoreCase(String anotherString) int compareTo(String anotherString), int compareToIgnoreCase(String str):
The result is a negative integer if this String object lexicographically precedes the argument string.
The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; startsWith(String prefix), startsWith(String prefix, int toffset),endsWith(String suffix)String toUpperCase(), String toLowerCase()String concat(String str):Concatenates the specified string to the end of this string. Similar to +.String replace(char oldChar, char newChar)
returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.String trim() returns a copy of the string, with leading and trailing whitespace omitted.
public class StringTest4 {
public static void main(String[] args) {
String s = "PIC10A is cool!";
System.out.println(s);
System.out.println(s.toLowerCase());
System.out.println(s.toUpperCase());
System.out.println(s.replace('o','a'));
System.out.println(s);
}
}
|
PIC10A is cool! pic10a is cool! PIC10A IS COOL! PIC10A is caal! PIC10A is cool! |
String. You can only assign a new reference to it.