1.String.length():返回字符串长度。
public class work1 {
public static void main(String[] args) {
String ret = "黄飞鸿";
System.out.println(ret.length()); // 3
}
}
2、String.charAt(int index):返回某索引处的字符
public class work1 {
public static void main(String[] args) {
String ret = "黄飞鸿";
System.out.println(ret.charAt(2)); // 鸿
}
}
3、String.isEmpty():判断字符串是否为空
public class work1 {
public static void main(String[] args) {
String ret = "黄飞鸿";
System.out.println(ret.isEmpty()); // false
}
}
4、String.toUpperCase():将字符转换成大写
public class work1 {
public static void main(String[] args) {
String ret = "abc";
System.out.println(ret.toUpperCase()); // ABC
}
}
5、String.toLowerCase():将字符转换成小写
public class work1 {
public static void main(String[] args) {
String ret = "ABC";
System.out.println(ret.toLowerCase()); // abc
}
}
6、String.equals(Object obj):比较字符串的内容是否相等
public class work1 {
public static void main(String[] args) {
String ret = "ABC";
String ret1 = "abc";
System.out.println(ret.equals(ret1)); // false
}
}
7、String.equalsIgnoreCase(String anotherString):忽略大小写比较字符串内容是否相等
public class work1 {
public static void main(String[] args) {
String ret = "ABC";
String ret1 = "abc";
System.out.println(ret.equalsIgnoreCase(ret1)); // true
}
}
8、String.trim():去除首尾空白字符
public class work1 {
public static void main(String[] args) {
String ret = " ABC";
String ret1 = "abc";
System.out.println(ret.trim()); // ABC
}
}
9、String.compareTo(String anotherString):比较两个字符串的大小
public class work1 {
public static void main(String[] args) {
String ret = "ABC";
String ret1 = "abc";
System.out.println(ret.compareTo(ret1)); // -32
}
}
10、String.substring(int beginIndex):返回一个新的字符串,它是原字符串从beginIndex开始截取到原字符串结尾的一个子字符串
public class work1 {
public static void main(String[] args) {
String ret = "ABC";
String ret1 = "abc";
System.out.println(ret.substring(1)); // BC
}
}
11、String.substring(int beginIndex,int endIndex):返回一个新的字符串,它是原字符串从beginIndex开始截取到endIndex(不包含)的一个子字符串
public class work1 {
public static void main(String[] args) {
String ret = "ABC";
String ret1 = "abc";
System.out.println(ret.substring(0,2)); // AB
}
}
12、String.endsWith(String suffix):测试此字符串是否以指定后缀结尾
public class work1 {
public static void main(String[] args) {
String ret = "ABC";
String ret1 = "abc";
System.out.println(ret.endsWith("C")); // true
}
}
13、String.startsWith(String prefix):测试此字符串是否以指定前缀开始
public class work1 {
public static void main(String[] args) {
String ret = "ABC";
String ret1 = "abc";
System.out.println(ret.startsWith("B")); // false
}
}
14、String.startsWith(String prefix,int toffset):测试此字符串是否从指定索引开始的子字符串是否以制定前缀开始
public class work1 {
public static void main(String[] args) {
String ret = "ABC";
String ret1 = "abc";
System.out.println(ret.startsWith("B",1)); // true
}
}
15、String.replace(char oldChar, char newChar):返回一个新的字符串,它是通过用newChar替换此字符串中出现的所有oldChar得到的。
public class work1 {
public static void main(String[] args) {
String ret = "ABC";
String ret1 = "abc";
System.out.println(ret.replace("A", "D")); // DBC
}
}
16、String.indexOf(String str):返回指定子字符串在此字符串中第一次出现处的索引
public class work1 {
public static void main(String[] args) {
String ret = "ABC";
String ret1 = "abc";
System.out.println(ret.indexOf("B")); // 1
}
}
17、String.indexOf(String str, int fromIndex):返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始
public class work1 {
public static void main(String[] args) {
String ret = "ABC";
String ret1 = "abc";
System.out.println(ret.indexOf("C",1)); // 2
}
}
18、String.lastindexOf(String str):返回指定子字符串在此字符串中最右边出现处的索引
public class work1 {
public static void main(String[] args) {
String ret = "ABC";
String ret1 = "abc";
System.out.println(ret.lastIndexOf("A")); // 2
}
}
19、String.lastIndexOf(String str, int fromIndex):返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索
public class work1 {
public static void main(String[] args) {
String ret = "ABC";
String ret1 = "abc";
System.out.println(ret.lastIndexOf("A",1)); // 0
}
}
20、String.concat():字符串的拼接
public class work1 {
public static void main(String[] args) {
String ret = "ABC";
String ret1 = "abc";
System.out.println(ret.concat(ret1)); // ABCabc
}
}