JAVA String类经常用方法
作者:翼风Ephonre tags:Java String str java String ing
1.charAt 方法,返回指定index的字符。 String string ="123456789"; char a =string.charAt(2); System.out.print(a); 2.codePointAt 方法,返回指定索引处的字符(Unicode 代码点)。 string ="abc"; int codePoint=string.codePointAt(2); System.out.print(codePoint); 3.codePointCount 字符的长度 引用: 在JDK5.0中,Java引入了对Unicode4.0中所谓"增补码"的支持,这种字符的长度大于2B,因此用char类型无法表示(Java中char类型占2B). Java的设计者于是利用两个char变量空间来表示这样的一个字符 于是,假如字符串中有这样的字符,则length方法无法精确的表示字符长度 (length方法返回的是String中char的个数) ,于是有了codePointCount方法,利用这个方法可以精确统计出String中字符的数量,考虑到了String中可能含有的"增补码" 4.compareTo 字符串的比较 假如2个字符串相同,返回值为0; string ="abcdefg"; int codePoint=string.compareTo("ea"); 将返回e在ea中的索引0减去e在“abcdefg”中的索引4,所以codePoint=-4 引用: 按字典顺序比较两个字符串。该比较基于字符串中各个字符的 Unicode 值。按字典顺序将此 String 对象表示的字符序列与参数字符串所表示的字符序列进行比较。假如按字典顺序此 String 对象位于参数字符串之前,则比较结果为一个负整数。假如按字典顺序此 String 对象位于参数字符串之后,则比较结果为一个正整数。假如这两个字符串相等,则结果为 0;compareTo 只在方法 equals(Object) 返回 true 时才返回 0。 这是字典排序的定义。假如这两个字符串不同,那么它们要么在某个索引处的字符不同(该索引对二者均为有效索引),要么长度不同,或者同时具备这两种情况。假如它们在一个或多个索引位置上的字符不同,假设 k 是这类索引的最小值;则在位置 k 上具有较小值的那个字符串(使用 < 运算符确定),其字典顺序在其他字符串之前。在这种情况下,compareTo 返回这两个字符串在位置 k 处两个char 值的差 5.compareToIgnoreCase 忽略大小写 6.indexOf(int ch)ch:unicode code point,假如字符串中没有ch,则返回-1String ss = "abcde"; System.out.println(ss.indexOf(2)); System.out.println(ss.indexOf(98)); 结果:-1 1 因为2对应的unicode在字符串ss中不存在,所以返回值-1,98对应的unicode 是b,所以返回值是index=1 7.indexOf(String str) 返回参数在字符串中第一次出现的位置索引 string ="abcaabc"; int codePoint=string.indexOf("bc"); 结果: index =1 8.indexOf(int ch, int fromIndex) ch (Unicode) String string ="abcaabc"; int codePoint=string.indexOf(99, 5); System.out.println(codePoint); System.out.println(string.indexOf(99, 5)); 结果: 6 2 因为c对应的Unicode为99 indexOf(String str,int fromIndex) 返回从索引fromIndex开始,str第一次出现的位置 String string ="abcaabc"; System.out.println(string.indexOf("bc", 0)); System.out.println(string.indexOf("bc", 5)); 结果: 1 5 lastIndexOf(int ch)返回 unicode 在字符串中最后出现时的位置索引 String string ="abcaabc"; System.out.println( 结果: 6 lastIndexOf(String str)返回 str 在字符串中最后出现时的位置索引 String string ="abcaabc"; System.out.println(string.lastIndexOf("bc")); 结果:5 lastIndexOf(int ch, int fromIndex)返回从0到fromIndex,ch最后出现的位置索引String string ="abcdafaa";System.out.println(string.lastIndexOf(97, 4)); 返回值<=fromIndex 结果:4 lastIndexOf(int ch, int fromIndex)返回从0到fromIndex,str最后出现的位置索引String string ="abcbcbcbcbc";System.out.println(返回值 k <= Math.min(fromIndex, str.length()) && this.startsWith(str, k)结果:4 9.offsetByCodePoints(int index,int codePointOffset) codePointOffset 偏移量 string ="abcaabcdef汉子"; int codePoint=string.offsetByCodePoints(8, 2); 结果: codePoint=10 引用解释: 一个完整的Unicode字符叫代码点/CodePoint,而一个Java char 叫代码单元code unit; string对象以UTF-16保存Unicode字符,需要用2个字符表示一个超大字符集汉字,这种表示方式为 Sruuogate,第一个字符叫Surrogate High,第二个就是Surrogate Low 判定一个char是否是Surrogate区的字符,用Character的isHighSurrogate()/isLowSurrogate()方法。 从两个Surrogate High/Low字符,返回一个完整的Unicode CodePoint用Character.toCodePoint()/codePointAt() 一个Code Point,可能需要一个也可能需要两个char表示,因此不能直接使用CharSequence.length() 方法返回一个字符串到底有多少个汉字,而需要用String.codePointCount()/Character.codePointCount() 要定位字符串中的第N个字符,不能直接将n作为偏移量,而需要从字符串头部依次遍历得到,需要 String.offsetByCodePoints() 从字符串的当前字符,找到上一个字符,不能直接用offset实现,而需要 String.codePointBefore(),或String.offsetByCodePoints() 从当前字符,找下一个字符,需要判定当前CodePoint的长度,再计算得到 String.offsetByCodePoints()。 个人测试:假如没有抛出异常,返回值总是index + codePointOffset 10.concat(String str)将参数连接到字符串的末尾 concatenate 如锁链般连续,使连锁,连结string ="abc";System.out.print(string.concat("123")); 结果:abc123 假如str的length是0,那么这个String就会被返回。 11.intern 字符串扣留 返回字符串对象的规范化表示形式。 java API解释:
String string1 = "Too many"; |
|