int length()
返回此字符串的长度。
public class 字符串长度 {
public static void main(String[] args) {
String x="qwertyuiopasdfghjklzxcvbnm";
System.out.println(x.length());
}
}
执行结果
26
isEmpty()
判断字符串是否为空。
public class 是否为空 {
public static void main(String[] args) {
String x="qwertyuiopasdfghjklzxcvbnm";
String y="";
System.out.println(x.isEmpty());
System.out.println(y.isEmpty());
}
}
执行结果
false
true
int indexOf(int ch)
返回指定字符在此字符串中第一次出现处的索引。
public class 返回字符串的下标 {
public static void main(String[] args) {
String string = "sbcdefghijklaopqrstuvwxyz";
//查找指定字符是在字符串中的下标。在则返回所在字符串下标;不在则返回-1.
System.out.println(string.indexOf("mn"));
// indexOf(String str); 返回结果:-1,"b"不存在
// 从第四个字符位置开始往后继续查找,包含当前位置
System.out.println(string.indexOf("s", 3));
//indexOf(String str, int fromIndex); 返回结果:17
//(与之前的差别:上面的参数是 String 类型,下面的参数是 int 类型)参考数据:a-97,b-98,c-99
// 从头开始查找是否存在指定的字符
System.out.println(string.indexOf(99));//indexOf(int ch);返回结果:2
System.out.println(string.indexOf('c'));//indexOf(int ch);返回结果:2
//从fromIndex查找ch,这个是字符型变量,不是字符串。字符a对应的数字就是97。
System.out.println(string.indexOf(97, 1));//indexOf(int ch, int fromIndex); 返回结果:12
System.out.println(string.indexOf('a', 1));//indexOf(int ch, int fromIndex); 返回结果:12
}
}
执行结果
-1
17
2
2
12
12
char charAt(int index)
返回指定索引处的 char 值。
public class 返回下标处字符串 {
public static void main(String[] args) {
String s = "www.weijun901.com";
char result = s.charAt(7,10);
System.out.println(result);
}
}
执行结果
j
contains(CharSequence chars)
判断是否包含指定的字符系列。
public class 是否包含字符串 {
public static void main(String[] args) {
String myStr = "Weijun901";
System.out.println(myStr.contains("Wei"));
System.out.println(myStr.contains("w"));
System.out.println(myStr.contains("j"));
}
}
执行结果
true
false
true
String concat(String str)
将指定字符串连接到此字符串的结尾。
public class 合并字符串 {
public static void main(String[] args) {
String s = "望天吼:";
s = s.concat("www.weijun901.com");
System.out.println(s);
}
}
执行结果
望天吼:www.weijun901.com
String replace(char oldChar, char newChar)
返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
public class 替换字符串 {
public static void main(String[] args) {
String Str = new String("Wei jun Wei jun");
System.out.print("返回值 :" );
System.out.println(Str.replace("Wei", "Jun"));
System.out.print("返回值 :" );
System.out.println(Str.replace("jun", "Wei"));
System.out.print("返回值 :" );
System.out.println(Str.replace(" ", ""));
}
}
执行结果
返回值 :Jun jun Jun jun
返回值 :Wei Wei Wei Wei
返回值 :WeijunWeijun
String[] split(String regex)
根据给定正则表达式的匹配拆分此字符串。
public class 拆分字符串 {
public static void main(String[] args) {
String str = new String("Welcome-to-Weijun901");
System.out.println("- 分隔符返回值 :" );
for (String retval: str.split("-")){
System.out.println(retval);
}
System.out.println("");
String str1 = new String("Welcome-to-Weijun901");
System.out.println("- 分隔符设置分割份数返回值 :" );
for (String retval: str1.split("-", 2)){
System.out.println(retval);
}
System.out.println("");
String str2 = new String("www.weijun901.com");
System.out.println("转义字符返回值 :" );
for (String retval: str2.split("\\.", 3)){
System.out.println(retval);
}
System.out.println("");
String str3 = new String("acount=? and uu =? or n=?");
System.out.println("多个分隔符返回值 :" );
for (String retval: str3.split("and|or")){
System.out.println(retval);
}
}
}
执行结果
- 分隔符返回值 :
Welcome
to
Weijun901
- 分隔符设置分割份数返回值 :
Welcome
to-Weijun901
转义字符返回值 :
www
weijun901
com
多个分隔符返回值 :
acount=?
uu =?
n=?
String substring(int beginIndex)
返回一个新的字符串,它是此字符串的一个子字符串。
public class 截取子串{
public static void main(String args[]) {
String Str = new String("www.weijun901.com");
System.out.print("返回值 :" );
System.out.println(Str.substring(4) );
System.out.print("返回值 :" );
System.out.println(Str.substring(4, 10) );
}
}
执行结果
返回值 :weijun901.com
返回值 :weijun
String toLowerCase()
使用默认语言环境的规则将此 String 中的所有字符都转换为小写。
public class 大写转小写 {
public static void main(String args[]) {
String Str = new String("WWW.WEIJUN901.COM");
System.out.print("返回值 :" );
System.out.println( Str.toLowerCase() );
}
}
执行结果
www.weijun901.com
String toUpperCase()
使用默认语言环境的规则将此 String 中的所有字符都转换为大写。
public class 小写转大写 {
public static void main(String[] args) {
String Str = new String("www.weijun901.com");
System.out.print("返回值 :" );
System.out.println( Str.toUpperCase() );
}
}
执行结果
返回值 :WWW.WEIJUN901.COM
boolean equals(Object anObject)
将此字符串与指定的对象比较。
public class 字符串内容是否相等 {
public static void main(String[] args) {
String Str1 = new String("Weijun901");
String Str2 = Str1;
String Str3 = new String("weijun901");
boolean retVal;
retVal = Str1.equals( Str2 );
System.out.println("返回值 = " + retVal );
retVal = Str1.equals( Str3 );
System.out.println("返回值 = " + retVal );
}
}
执行结果
返回值 = true
返回值 = false
int compareTo(String anotherString)
按字典顺序比较两个字符串。
public class 比较字符串 {
public static void main(String[] args) {
String str1 = "Weijun";
String str2 = "Weijun";
String str3 = "Weijun901";
int result = str1.compareTo( str2 );
System.out.println(result);
result = str2.compareTo( str3 );
System.out.println(result);
result = str3.compareTo( str1 );
System.out.println(result);
}
}
执行结果
0
-3
3