java string类的方法_转 JAVA String类常用方法

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解释:

public String intern()

一个初始时为空的字符串池,它由类 String 私有地维护。

当调用 intern 方法时,如果池已经包含一个等于此 String 对象的字符串(该对象由

equals(Object) 方法确定),则返回池中的字符串。否则,将此

String 对象添加到池中,并且返回此 String 对象的引用。

它遵循对于任何两个字符串 s 和 t,当且仅当

s.equals(t) 为 true

时,s.intern() == t.intern()

才为true。

String string1 = "Too many";

String string2 = " cooks";

String string3="Too many cooks";

String string4 =string1 +string2;

System.out.println(string3==string4);

string4=string4.intern();

System.out.println(string3==string4);

结果:

false

true

12 hashCode 返回字符串的hashCode值

String string0 ="abc";

String string1 ="abc";

System.out.println(string1.hashCode());

System.out.println(string1.hashCode());

结果:

96354

96354

13 contains(CharSequence

s)是否包含参数

String string0 ="abcdef";

System.out.println( string0.contains("de"));

结果:true

14 contentEquals(CharSequence

cs)

将此字符串与指定的 CharSequence

比较。当且仅当此 String 与指定序列表示相同的 char 值序列时,结果才为

true。 参数 cs -

要与此 String 比较的序列返回:如果此

String 与指定序列表示相同的 char 值序列,则返回 true;否则返回

false。

String string0 ="abcdef";

System.out.println(

string0.contentEquals("abcdef"));结果:true

15 contentEquals(StringBuffer sb)

将此字符串与指定的 StringBuffer

比。相同,true,否则:false

String string0 ="abcdef";

System.out.println( string0.contentEquals(new

StringBuffer("abcdef")));

结果:true

16 getChars(int srcBegin, int srcEnd, char[] dst, int

dstBegin)

将当前字符串的部分字符复制到目标自负数组dst中,从srcBegin(包含在内)到srcEnd(不包含在内)之间的字符复制到目标字符数组中的字符从dstBegin位置开始存放.

String ss="hello,word";

char dst[]={'a','b','c','d','e','f'};

ss.getChars(4,6,dst,3);

System.out.println(dst);

结果

abco,f

17.getBytes(String charsetName);

String ss = "abcde";

byte dst[] = new byte[5];

try {

dst = ss.getBytes("GB2312");

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

for (int i = 0; i < dst.length; i++) {

System.out.print(i==4?dst[i]:dst[i]+",");

}

}

结果:97,98,99,100,101

18 getBytes

String ss = "abcde";

byte dst[] = new byte[5];

dst = ss.getBytes();

for (int i = 0; i < dst.length; i++) {

System.out.print(i==4?dst[i]:dst[i]+",");

}

结果:97,98,99,100,101

19 startsWith(String perfix) 是否以perfix开头,yes

返回true ,no返回false

String string ="abcbd";

System.out.println( string.startsWith("abc"));

System.out.println( string.startsWith("Ab"));

结果:true

false

startsWith(String

prefix, int

toffset)偏移toffset,是否以perfix开头,yes

返回true ,no返回false

String string ="abcde";

System.out.println( string.startsWith("cd", 2));

包含toffset

对应的字符串

结果:true

20 endsWith(String

suffix)是否以suffix结尾,yes 返回true

,no返回false

String string ="abcde";

System.out.println( string.endsWith("e"));结果:true

21 trim()去掉字符串的前后空格

String string =" abc ";

System.out.println(string.length()+","+ string.trim().length());

结果:5,3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值