String 方法汇总
length()
得到字符串的长度
String str = "abc";
System.out.println(str.length());//3
matcher(regex)
匹配正则表达式(regex正则表达式)
String str = "abc";
System.out.println(str.matches("(.*)c"));//true
compareTo(String s)
比较两个字符串的大小
String str = "abc";
System.out.println(str.compareTo("abcd"));//-1 比较每个字符,如前面都相同,而其中一个
//已到末尾,则返回两字符串长度相减
System.out.println(str.compareTo("aba"));//2 结果为c-a = 2;
charAt(int index)
返回字符串下标为index的字符
String str = "abc";
System.out.println(str.charAt(1));//b
isBlank()
判断字符串是否为空白与空有区别,不可判断null,否则会空指针异常
System.out.println(" ".isBlank());//true
System.out.println("\n".isBlank());//true
System.out.println("\r".isBlank());//true
System.out.println("?".isBlank());//false
isEmpty()
判断字符串是否为空,不可判断null,否则会空指针异常
System.out.println("".isEmpty());//true
System.out.println(" ".isEmpty());//false
System.out.println("\n".isEmpty());//false
System.out.println("\r".isEmpty());//false
equals(String str)
判断两个字符串是否相等,其底层是==,但是其对String比较的时候改为了值比较
String str = "abc";
String s = new String("abc");
System.out.println(str.equals("abc"));//true
System.out.println(str.equals(s));//true
System.out.println(str==s);//false
concat(String str)
拼接两个字符串
String str = "abc";
String s = new String("abd");
System.out.println(str.concat(s));//abcabd
chars()
??? 暂时不知道干什么用的,官方解释是返回一个由这个序列中的char值组成的IntStream
String str = "abc";
System.out.println(str.chars());//java.util.stream.IntPipeline$Head@43588265
codePointAt(int index)
返回index下标字符的索Unicode码
String str = "abc";
System.out.println(str.codePointAt(1));//98
compareToIgnore(String str)
按字典序比较两个字符串,不考虑大小写
String str = "abc";
String s = "ABC";
System.out.println(str.compareToIgnoreCase(s));//0
System.out.println(str.compareTo(s));//32
contains(CharSequence s)
判断字符串是否包含了s
String str = "abc";
System.out.println(str.contains("ab"));//true
System.out.println(str.contains("d"));//false
contentEquals(CharSequence cs)
判断字符串内容是否相等
String str = "abc";
StringBuffer s = new StringBuffer("abc");
System.out.println(str.contentEquals(s));//true
System.out.println(str.equals(s));//false
endWith(String suffix)
判断一个字符串是否以suffix结尾
String str = "abc";
System.out.println(str.endsWith("c"));//true
System.out.println(str.endsWith("a"));//false
startsWith(String prefix,int toffset)
判断一个字符串是否以prefix开始
String str = "abc";
System.out.println(str.startsWith("c"));//false
System.out.println(str.startsWith("a"));//true
System.out.println(str.startsWith("c",2));//true toffset表示偏移量
getBytes()
返回一个byte数组,使用平台的默认字符集将字符串编码为 byte 序列 可选参数CharSet charset,String charsetName,使用指定的字符集将字符串编码为byte序列
String str = "abc";
byte[] b = str.getBytes();
for (int i = 0; i < b.length; i++) {
System.out.print(b[i]);//979899
}
System.out.print(b)//[B@5b37e0d2
System.out.println(str.getBytes("UTF-8"));//[B@25de8898
hashCode()
获取当前字符串的hash值
String str = "abc";
System.out.println(str.hashCode());//96354
indexOf(char ch)
返回字符串中首次出现ch的下标
String str = "abc";
System.out.println(str.indexOf('c'));//2
System.out.println(str.indexOf(98));//1 //int 类型表示Unicode编码值
System.out.println(str.indexOf("bc"));//1 //String类型查找字符串
System.out.println(str.indexOf(99,1));//2 //int int表示从下标1开始查找99
System.out.println(str.indexOf('c',1));//2 //String int表示从下标1开始查找c
lastIndexOf()
返回字符串中最后一次出现ch的下标
String str = "abc";
System.out.println(str.lastIndexOf('c'));//2
System.out.println(str.lastIndexOf(98));//1 //int 类型表示Unicode编码值
System.out.println(str.lastIndexOf("bc"));//1 //String类型查找字符串
System.out.println(str.lastIndexOf(99,1));//2 //int int表示从下标1开始查找99
System.out.println(str.lastIndexOf('c',1));//2 //String int表示从下标1开始查找c
intern()
intern() 方法返回字符串对象的规范化表示形式
在调用”ab”.intern()方法的时候会返回”ab”,但是这个方法会首先检查字符串池中是否有”ab”这个字符串,如果存在则返回这个字符串的引用,否则就将这个字符串添加到字符串池中,然会返回这个字符串的引用
String str1 = "a";
String str2 = "b";
String str3 = "ab";
String str4 = str1 + str2;
String str5 = "a" + "b";
String str6 = new String("ab");
System.out.println(str6.equals(str3));//true
System.out.println(str6 == str3);//false
System.out.println(str6.intern() == str3);//true
System.out.println(str6.intern() == str4);//fasle
System.out.println(str6.intern() == str5);//true
//!!!这里可以看到str4和str5都是拼接成的ab,为什么在比较时结果就不一样呢?
//字符串相加的时候,都是静态字符串的结果会添加到字符串池(str5),如果其中含有变量(str4)则不会进入字符串池中。
//但是字符串一旦进入字符串池中,就会先查找池中有无此对象。
//如果有此对象,则让对象引用指向此对象。如果无此对象,则先创建此对象,再让对象引用指向此对象。
lines()
通过其行终止符分隔字符串,并返回由这些行终止符分隔的字符串流
String str = "abc";
//使用方法暂时还没搞清楚😀
offsetByCodePoints(int index , int codePointOffset)
返回此 String 中从给定的 index 处偏移 codePointOffset 个代码点的索引
String str = "abc";
System.out.println(str.offsetByCodePoints(1,1));//2
regionMatches(int toffset, String other,int ooffset,int len)
用于检测两个字符串在一个区域内是否相等
String str = "abcdefg";
String str1 = "cvefg";
System.out.println(str.regionMatches(4,str1,2,3));//true 比较的是str的下标为4-6的区间与str1的下标为2-4的区间,即efg
//int toffset 表示字符串起始偏移量
//String other 要比较的字符串
//int ooffset 比较字符串的起始偏移量
//int len 比较子字符串的长度
//boolean ignoreCase 为true时忽略大小写
//regionMatches(boolean ignoreCase, int toffset, String other,int ooffset,int len)
str1 = "CVEFG"
System.out.println(str.regionMatches(true,4,str1,2,3));//true
System.out.println(str.regionMatches(false,4,str1,2,3))//false
repeat(int count)
返回一个字符串重复count次
String str = "abc";
System.out.println(str.repeat(2));//abcabc
System.out.println(str.repeat("2"));//abcabc String 会转换为int 如"ss",则无输出
System.out.println(str.repeat(0.9));// 无输出,小数部会省去
System.out.println(str.repeat(NaN));// 无输出,NaN当成0处理
System.out.println(str.repeat(-0.9));// 无输出,小数部分省去,-0还是0
System.out.println(str.repeat(-1));// 报错
System.out.println(str.repeat(Infinity));// 报错
replace(char oldChar, char newChar)
使用newChar替换字符串中的所有oldChar字符
String str = "abcabc123";
System.out.println(str.replace("a","e"));//ebcebc
//replace(CharSequence target, CharSequence replacement) //使用字符串替换
replaceAll(String regex, String replacement) //使用replacement替换正则表达式匹配的内容,也可以作为replace来使用
String regex = "\\d+";
System.out.println(str.replace(regex,"z"));//abcabczzz
replaceFirst(String regex, String replacement) //使用replacement替换正则表达式匹配的第一个内容
System.out.println(str.replaceFirst("a","d"));//dbcabc
split(String regex,int limit)
以regex为分隔符分割字符串,返回一个String数组
String str = "abc";
System.out.println(Arrays.toString(str.split("")));//[a, b, c]
System.out.println(Arrays.toString(str.split("",2)));//[a, bcabc] limit表示分割limit - 1次
trim()
返回一个字符串,移除字符串首尾的空白字符(空格、\t、\n)
String str = " abc ";
System.out.println(str.trim());//abc
strip()
返回一个字符串,移除字符串首尾的空白字符(空格、\t、\n)
trim ()和 strip ()功能类似,主要 区别 是: trim ()可以去除字符串前后的半角空白字符 strip ()可以去除字符串前后的全角和半角空白字符 半角和全角 半角是我们多数人在打字的时候使用的状态,如果我们不去刻意调整半全角,它会一直伴随着我们,因为半角状态下,人们已经习惯了这种打字模式,半角状态下可以使用任何标点符号,而且对于空格也没有特殊限制。
String str = "abc";
System.out.println(str.strip());//abc
substring(int beginIndex, int endIndex)
截取从beginIndex到endIndex区间的字符串(不包括endIndex)
String str = "abcdefg";
System.out.println(str.substring(2,4));//cd
System.out.println(str.substring(2));//cdefg 一个参数,则从下标2开始截取完剩下的
toCharArray()
返回一个char数组,将字符串转换为char数组
String str = "abc";
System.out.println(str.toCharArray()); //abc
toLowerCase()
转换为小写字母
String str = "ABC";
System.out.println(str.toLowerCase());//abc
toUpperCase()
转换为大写字母
String str = "abc";
System.out.println(str.toLowerCase());//ABC