JAVA常见字串函数.docVIP

  • 3
  • 0
  • 约 7页
  • 2016-10-06 发布于贵州
  • 举报
JAVA常见字串函数

常见字符串函数 函数名 格式 功能 charAt charAt(int?index) 返回指定位置上的字符 实例 String str=Hello,World!; System.out.println(str.charAt(1)); 结果 e compareTo compareTo(String str) 按ASCII顺序比较字符串大小,返回字符ASCII值之差, 返回值大于0,表示大于 返回值等于0,表示相等 返回值小于0,表示小于 实例 String str=Hello,World!; System.out.println(pareTo(I)); 结果 -1,表示Hello,World!小于”I” compareToIgnoreCase compareToIgnoreCase(String str) 按ASCII顺序比较字符串大小,返回字符ASCII值之差,但忽略大小写 返回值大于0,表示大于 返回值等于0,表示相等 返回值小于0,表示小于 实例 String str=Hello,World!; System.out.println( pareToIgnoreCase(hello,world!)); 结果 0,表示忽略大小写,两个字符串相等 Concat concat(String str) 将指定字符串联到此字符串的结尾 实例 String str=Hello,World!; System.out.println(str.concat(ok)); 结果 Hello,World!ok copyValueOf copyValueOf(char[] ch,int start,int length) 从字符数组指定位置复制指定个数的字符 实例 String str=Hello,World!; char[] ch={H,e,l,l,o}; System.out.println(str.copyValueOf(ch,0,2)); System.out.println(str); 结果 He Hello,World! copyValueOf copyValueOf(char[],ch) 复制字符数组中的字符 实例 String str=Hello,World!; char[] ch={H,e,l,l,o}; System.out.println(str.copyValueOf(ch)); System.out.println(str); 结果 Hello Hello,World! endsWith endsWith(String str) 测试是否以指定字符串结尾 实例 String str=Hello,World!; System.out.println(str.endsWith(!)); System.out.println(str.endsWith(d)); 结果 true false 函数名 格式 功能 equals equals(Object obj) 测试字符串是否相等 实例 String str=Hello,World!; System.out.println(str.equals(ok)); 结果 false equalsIgnoreCase equalsIgnoreCase(String str) 测试字符串是否相等,但忽略大小写 实例 String str=Hello,World!; System.out.println(str.equalsIgnoreCase(hello,world!)); 结果 True getBytes getBytes() 获取字符串对应的字节数组 实例 String str=Hello,World!; byte[] b=new byte[20]; b=str.getBytes(); //通过字节数组生成字符串 System.out.println(new String(b)); 结果 Hello,World! getChars getChars(int srcBegin,int srcEnd,char[] dst,int dstBegin) 从字符串中指定开始位置到结束位置之前的所有字符复制到字符数组中 实例 char[] ch=new char[5]; String str=Hello,World!; str.getChars(0,5,ch,0); System.out.println(new String(ch)); 结果 Hello indexOf indexOf(int?ch) 返回指定字符在此字符串中第一次出现处的索引 实例 String str=Hello,World!; S

文档评论(0)

1亿VIP精品文档

相关文档