public int length():返回字符串长度。
package Demo05;
public class TestString {
public static void main(String[] args) {
String str = "woshiguhuo";
System.out.println(str.length()); // 10
}
}
public char charAt(int index):依据下标获取字符。
package Demo05;
public class TestString {
public static void main(String[] args) {
String str = "woshiguhuo";
char[] strArrays = new char[str.length()];
for (int i = 0; i < strArrays.length; i++) {
strArrays[i] = str.charAt(i);
}
System.out.println(strArrays); // woshiguhuo
}
}
public boolean contains(String str):判断当前字符串中是否包含str。
package Demo05;
public class TestString {
public static void main(String[] args) {
String str = "woshiguhuo";
if (str.contains("guhuo")) {
System.out.println("guhuo is contained!"); // guhuo is contained!
} else {
System.out.println("guhuo is not contained");
}
}
}
package Demo05;
public class TestString {
public static void main(String[] args) {
String str = "woshiguhuo";
char[] charArray = str.toCharArray();
for (int i = 0; i < charArray.length; i++) {
System.out.print(charArray[i]+" ");
/**
* w o s h i g u h u o
* */
}
}
}
public int indexOf(String str):查找str首次出现的下标,存在,则返回该下标;不存在,则返回-1;
package Demo05;
public class TestString {
public static void main(String[] args) {
String str = "woshiguhuo";
System.out.println(str.indexOf("wo"));
System.out.println(str.indexOf("shi"));
System.out.println(str.indexOf("guhuo"));
}
}
/**
* 0
* 2
* 5
* */
package Demo05;
public class TestString {
public static void main(String[] args) {
String str = "woshiguhuo";
System.out.println(str.lastIndexOf("wo"));
System.out.println(str.lastIndexOf("shi"));
System.out.println(str.lastIndexOf("guhuo"));
}
}
/**
* 0
* 2
* 5
* */
package Demo05;
public class TestString {
public static void main(String[] args) {
String str = " woshiguhuo ";
System.out.println(str.trim()); // woshiguhuo
}
}
public String toUpperCase():将小写转成大写.
package Demo05;
import java.util.Locale;
public class TestString {
public static void main(String[] args) {
String str = "woshiguhuo";
String str1 = str.toUpperCase();
System.out.println(str1); // WOSHIGUHUO
System.out.println(str1.toLowerCase()); // woshiguhuo
}
}
public boolean endsWith(String str):判断字符串是否以str结尾。
package Demo05;
public class TestString {
public static void main(String[] args) {
String str = "woshiguhuo";
if (str.endsWith("guhuo")) {
System.out.println("str end with guhuo"); // str end with guhuo
} else {
System.out.println("str end without guhuo");
}
}
}
package Demo05;
public class TestString {
public static void main(String[] args) {
String str = "woshiguhuo";
String str1 = str.replace("guhuo","mazi");
System.out.println(str1); // woshimazi
}
}
public String[] split(String str):根据str做拆分。
package Demo05;
public class TestString {
public static void main(String[] args) {
String str = "wo shi gu huo";
String[] chars = str.split(" ");
for (int i = 0; i < chars.length; i++) {
System.out.print(chars[i]+"/"); //wo/shi/gu/huo/
}
}
}
StringBuffer和StringBuilder之间的区别:前者线程安全,后者线程不安全;前者效率低,后者效率高。
package Demo05;
public class testStringBuffer {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer();
StringBuilder stringBuilder = new StringBuilder();
}
}
append()
package Demo05;
public class testStringBuffer {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("woshi");
StringBuilder stringBuilder = new StringBuilder("nishi");
stringBuffer.append("guhuo");
stringBuilder.append("mazi");
System.out.println(stringBuffer); // woshiguhuo
System.out.println(stringBuilder); // nishimazi
}
}
insert(int index, str String)
package Demo05;
public class testStringBuffer {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("woshi");
StringBuilder stringBuilder = new StringBuilder("nishi");
stringBuffer.append("guhuo");
stringBuilder.append("mazi");
stringBuffer.insert(0,"dajiahao!");
stringBuilder.insert(9,"!");
System.out.println(stringBuffer); // dajiahao!woshiguhuo
System.out.println(stringBuilder); // nishimazi!
}
}
delet(int startIndx,int endIndex)和deleteCharAt(int Index)
package Demo05;
public class testStringBuffer {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("woshi");
StringBuilder stringBuilder = new StringBuilder("nishi");
stringBuffer.append("guhuo");
stringBuilder.append("mazi");
stringBuffer.insert(0,"dajiahao!");
stringBuilder.insert(9,"!");
stringBuffer.delete(0,5);
stringBuilder.deleteCharAt(9);
System.out.println(stringBuffer); // hao!woshiguhuo
System.out.println(stringBuilder); // nishimazi
}
}
replace的endIndex是在其后一位!!!
package Demo05;
public class testStringBuffer {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("woshi");
StringBuilder stringBuilder = new StringBuilder("nishi");
stringBuffer.replace(0,2,"ni"); // 注:endIndex是在其后一位!!!
stringBuilder.replace(0,2,"wo");
System.out.println(stringBuffer); // nishi
System.out.println(stringBuilder); // woshi
}
}
toString()
package Demo05;
public class testStringBuffer {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("woshi");
StringBuilder stringBuilder = new StringBuilder("nishi");
System.out.println(stringBuffer.toString().getClass()); // class java.lang.String
System.out.println(stringBuilder.toString().getClass()); // class java.lang.String
}
}