String的常用方法
charAt():获取指定下标位置的字符(返回指定索引处的字符)
indexOf():返回目标字符在字符串中的位置下标(返回指定字符第一次出现位置的索引)
lastIndexOf(): 返回指定字符最后一次出现位置的索引
length():返回字符串的长度
split(): 字符串分割
trim():去除前后空格
valueOf(): 将其他类型转换为字符串类型
substring():截取字符串
toLowerCase():字符串转小写
toUpperCase():字符串转大写
contains():是否包含目标字符串
startsWith():是否以目标字符串开头
endsWith():是否已目标字符串结束
isEmpty():字符串的长度是否为0,length()==0结果为true
replace(): 字符串替换
equals():字符串比较
getBytes():返回字符串的byte类型数组
使用方法
charAt()
String s = "abc";
char s2= s.charAt(1);
System.out.println(s2); //b
indexOf()
String s = "abccc";
int s2 = s.indexOf("c");
System.out.println(s2); //2
lastIndexOf()
String s = "abccc";
int s2 = s.lastIndexOf("c");
System.out.println(s2); //4
length()
String s = "abc";
int s2 = s.length();
System.out.println(s2); //3
split()
String s = "123&456";
String[] values = s.split("&");
String s1 = values[0];
String s2 = values[1];
System.out.println(s1); //123
System.out.println(s2); //456
trim()
String s = " ab c ";
String s2 = s.trim();
System.out.println(s2); //ab c
valueOf()
int类型转String类型
int i = 5;
String s = String.valueOf(s);
System.out.println(s); // "5"
String类型转int类型
String s = "123";
int i = Integer.parseInt(s);
System.out.println(i); // 123
substring()
用法1:
String s = "abcdefg";
String s2 = s.substring(3);
System.out.println(s2); //defg
用法2:
String s = "abcdefg";
String s2 = s.substring(2,4); //[2,4)
System.out.println(s2); //cd
toLowerCase()
String s = "ABCabc";
String s2 = s.toLowerCase();
System.out.println(s2); //abcabc
toUpperCase()
String s = "ABCabc";
String s2 = s.toUpperCase();
System.out.println(s2); //ABCABC
contains()
//注意:此方法区分大小写。
String s1 = "ABCDE";
String s2 = "CD";
System.out.println(s1.contains(s2)); //true
startsWith()
String s = "abcd";
System.out.println(s.startsWith("ab")); //true
endsWith()
String s = "abcd";
System.out.println(s.endsWith("d")); //true
isEmpty()
String s = "";
boolean s2 = s.isEmpty();
System.out.println(s2); //true
replace()
String s = "abcd";
String s2 = s.replace("b","F");
System.out.println(s2); //aFcd
equals()
String s1 = "abc";
String s2 = "abc";
System.out.println(s1.equals(s2)); //true
组合使用
获取图片名称后缀
String s = "123.jpg";
int s1 = s.lastIndexOf(".");
String s2 = s.substring(s1);
System.out.println(s2); // .jpg
java8-Api
学习文档:https://www.matools.com/api/java8