关于String的常用方法:
1.charAt(int index):返回指定索引处的值:
public static void main(String[] args) { String str = "abcf"; System.out.println(str.charAt(0));// a }
2.concat(String str):将指定的字符串连接到该字符串的末尾
public static void main(String[] args) { String str = "abcf"; System.out.println(str.concat("llll"));// abcfllll }
3.equals(Object o):将此字符串与指定对象进行比较
public static void main(String[] args) { String str1 = "abcf"; String str2 = "abcf"; System.out.println(str1.equals(str2));// true }
4.length():返回字符串的长度
public static void main(String[] args) { String str1 = "abcf"; System.out.println(str1.length());// 4 }
5.indexOf(String str):返回指定字符串第一次出现在字符串内的索引
public static void main(String[] args) { String str1 = "abcf"; System.out.println(str1.indexOf("bc"));// 1 }
6.replace(char oldChar,char newChar):将指定字符串替换为新的字符串
public static void main(String[] args) { String str1 = "abcf"; System.out.println(str1.replace("a","ccc"));// cccbcf }
7.split():字符串分割
public static void main(String[] args) { String str = "ssssefefdf"; String[] split = str.split(""); System.out.println(Arrays.toString(split));//[s, s, s, s, e, f, e, f, d, f] }
8.subString(int beginIndex):字符串截取
public static void main(String[] args) { String str = "ssssefefdf"; String substring = str.substring(1); System.out.println(substring);//sssefefdf }
9.toCharArray():将字符串转化为一个字符数组
public static void main(String[] args) { String str1 = "abcf"; char[] a = str1.toCharArray(); System.out.println(a);// abcf }
10.toUpperCase():将字符串转化为大写:
public static void main(String[] args) { String str = "SSsefefdf"; System.out.println(str.toUpperCase());// SSSEFEFDF }
11.toLowerCase():将字符串转化为小写:
public static void main(String[] args) { String str = "SSsefefdf"; System.out.println(str.toLowerCase());// sssefefdf }
12.valueOf(int a):int类型转字符串(这里可以是char,double,float等)
public static void main(String[] args) { int a = 90; String str = String.valueOf(a); System.out.println(str);//90 }