1.字符查询
charAt(int index) | 查找index位置的字符 |
indexOf(String str) | 查找字符或者字符串在该字符串中从左边首次出现的位置,并返回 |
indexOf(String str,int fromIndex) | 查找某字符或者字符串在该字符串中的从fromIndex起开始出现的位置,并返回 |
lastIndexOf(String str) | 与IndexOf的区别是从末尾开始返回 |
lastIndexOf(String str,int fromIndex) | 与IndexOf(String str,int fromIndex)的区别是从末尾开始返回 |
contains(CharSequence s) | 判断字符串是否包含s值,包含返回true |
public class Main {
public static void main(String[] args) {
String str = "12345678";
System.out.println(str.charAt(2));
System.out.println(str.indexOf("3"));
System.out.println(str.contains("3"));
}
}
输出结果:
3
2
true
2.字符串连接
concat(String str) | 字符串拼接,将str连接到字符串后面,相当于“+” |
3.字符串拆分
split("") | 字符串拆分。根据双引号中内容进行拆分 |
4.字符串比较
endsWith(String suffix) | 判断该字符串是否是以suffix结束 |
startsWith(String prefix) | 判断该字符串是否以prefix前缀开始 |
compareTo(String anString) | 进行字符串比较 返回正数,则String大于anString 返回负数,则String小于anString 返回0,则String等于anString |
compareTolgnore(String anString) | 与compareTo相似,但忽略大小 |
equals(Object anobject) | 比较两个字符串内容 |
equalsIgnoreCase(String anString) | 与equals方法相似,但忽略大小 |
regionMatches(boolean b,int firstStart,String other,int otherStart,intlength) | 从当前字符串的firstStart位置开始比较,取长度为length的一个子字符串,other字符串从otherStart位置开始,指定另外一个长度为length的字符串,两字符串比较,当b为true时字符串不区分大小写。 |
public class Main {
public static void main(String[] args) {
String stra = "123";
String strb = "345";
System.out.println(stra.compareTo(strb));
System.out.println(strb.compareTo(stra));
System.out.println(stra.compareTo(stra));
}
}
输出结果:
-2
2
0
5.字符串转换为数组
split(String regex) | 以regex为分隔符,将Sring字符串转化为String数组(支持正则) |
toCharArray() | 将字符串转化为char字符数组 |
format(Locale l,String format,object..args) | 格式化字符串 |
getBytes(String charset) | 将string转换为byte数组 |
getChars(int srcBegin,int srcEnd,char[] dst,int dstBegin) | 将string转换为字符数组 |
public class Main {
public static void main(String[] args) {
String stra = "1 2 3 4 5 6 7 8 ";
String[] strarg = stra.split(" ");
for (String a : strarg){
System.out.print(a);
}
System.out.println("");
String strb = "1a2b3c4d5h6f";
String[] strbrg = strb.split("\\d");
for(String b : strbrg){
System.out.print(b);
}
}
}
输出结果:
12345678
abcdhf
6.字符串替换
matches(String regex) | 判断该字符串是否匹配给定的正则表达式 |
replace(char oldChar,char newChar) | 将所有的oldString替换成newString |
replaceFirst(String regex,String replacement) | 与replace相似,用replacement替换regex相匹配的第一个字符串,返回新的字符串 |
replaceAll(String regex,String replacement) | 与replace相似,用replacement替换regex相匹配的所有字符串,并且返回新的字符串 |
replace和replaceAll的最大区别在于,replaceAll可以使用正则表达式进行筛选
public class Main {
public static void main(String[] args) {
String stra = "1 2 3 4 5 6 7 8 ";
String strarg = stra.replace(" ","");
System.out.println(strarg);
String strb = "1a2b3c4d5h6f";
String strbrg = strb.replaceAll("\\d","");
System.out.println(strbrg);
}
}
输出结果:
12345678
abcdhf
7.StringBuffer常用方法
append(基础类型数据 b) | 将基础类型数据拼接到字符后面 |
delete(int start,int end) | 移除此序列的字符串中的字符 |
deleteCharAt(int index) | 移除此序列指定的位置 |
insert(int offset,基础类型数据 b) | 将基础数据类型数据插入的到指定位置 |
toString() | 将此序列的数据转换为字符串返回 |
8.其他
valueOf(基础数据类型 b) | 将数据基本类型数据转换为字符串 |
substring(int beginIndex) | 该方法从beginIndex位置截取剩余字符串并返回 (从0开始,[beginindex,~)) |
substring(int beginIndex,int endIndex) | 该方法从beginIndex位置截取到endIndex位置并返回 (从0开始,[beginindex,endIndex)) |
trim() | 去除字符串两边空格 |
isEmpty() | 当length()为0时返回true |
toLowerCase() | 将String中所有的字符都转换成小写 |
toUpperCase() | 将String中所有字符都转换成大写 |
public class Main {
public static void main(String[] args) {
int a = 12345;
System.out.println(String.valueOf(a));
System.out.println("--------------------------------------------------");
String str = "123456789";
System.out.println(str.substring(5));
System.out.println(str.substring(1,5));
System.out.println("--------------------------------------------------");
String strb = null;
String strc = "";
// System.out.println(strb.isEmpty()); 会报空指针
System.out.println(strc.isEmpty());
}
}
输出结果
12345
--------------------------------------------------
6789
2345
--------------------------------------------------
true