JAVA常见字符串函数.doc

上传人:自*** 文档编号:126231996 上传时间:2020-03-23 格式:DOC 页数:7 大小:80.50KB
返回 下载 相关 举报
JAVA常见字符串函数.doc_第1页
第1页 / 共7页
JAVA常见字符串函数.doc_第2页
第2页 / 共7页
JAVA常见字符串函数.doc_第3页
第3页 / 共7页
JAVA常见字符串函数.doc_第4页
第4页 / 共7页
JAVA常见字符串函数.doc_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《JAVA常见字符串函数.doc》由会员分享,可在线阅读,更多相关《JAVA常见字符串函数.doc(7页珍藏版)》请在金锄头文库上搜索。

1、常见字符串函数函数名格式功能charAtcharAt(intindex)返回指定位置上的字符实例String str=Hello,World!;System.out.println(str.charAt(1);结果ecompareTocompareTo(String str)按ASCII顺序比较字符串大小,返回字符ASCII值之差,返回值大于0,表示大于返回值等于0,表示相等返回值小于0,表示小于实例String str=Hello,World!;System.out.println(pareTo(I);结果-1,表示Hello,World!小于”I”compareToIgnoreCaseco

2、mpareToIgnoreCase(String str)按ASCII顺序比较字符串大小,返回字符ASCII值之差,但忽略大小写返回值大于0,表示大于返回值等于0,表示相等返回值小于0,表示小于实例String str=Hello,World!;System.out.println(pareToIgnoreCase(hello,world!);结果0,表示忽略大小写,两个字符串相等Concatconcat(String str)将指定字符串联到此字符串的结尾实例String str=Hello,World!;System.out.println(str.concat(ok);结果Hello,W

3、orld!okcopyValueOfcopyValueOf(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);结果HeHello,World!copyValueOfcopyValueOf(char,ch)复制字符数组中的字符实例String str=Hello,World!;char ch=H,e,l,l,o;System.out.

4、println(str.copyValueOf(ch);System.out.println(str);结果HelloHello,World!endsWithendsWith(String str)测试是否以指定字符串结尾实例String str=Hello,World!;System.out.println(str.endsWith(!);System.out.println(str.endsWith(d);结果truefalse函数名格式功能equalsequals(Object obj)测试字符串是否相等实例String str=Hello,World!;System.out.print

5、ln(str.equals(ok);结果falseequalsIgnoreCaseequalsIgnoreCase(String str)测试字符串是否相等,但忽略大小写实例String str=Hello,World!;System.out.println(str.equalsIgnoreCase(hello,world!);结果TruegetBytesgetBytes()获取字符串对应的字节数组实例String str=Hello,World!;byte b=new byte20;b=str.getBytes();/通过字节数组生成字符串System.out.println(new Str

6、ing(b);结果Hello,World!getCharsgetChars(int srcBegin,int srcEnd,char dst,int dstBegin)从字符串中指定开始位置到结束位置之前的所有字符复制到字符数组中实例char ch=new char5;String str=Hello,World!;str.getChars(0,5,ch,0);System.out.println(new String(ch);结果HelloindexOfindexOf(intch)返回指定字符在此字符串中第一次出现处的索引实例String str=Hello,World!;System.ou

7、t.println(str.indexOf(o);结果4indexOfindexOf(intch, intfromIndex)从指定的索引开始从前往后搜索,返回在此字符串中第一次出现指定字符处的索引实例String str=Hello,World!;System.out.println(str.indexOf(o,6);结果7函数名格式功能lastIndexOflastIndexOf(int ch)返回最后一次出现的指定字符在此字符串中的索引实例String str=Hello,World!;System.out.println(str.lastIndexOf(o);结果7lastIndexO

8、flastIndexOf(int ch,int fromIndex)从指定的索引处开始进行从后向前搜索,返回最后一次出现的指定字符在此字符串中的索引实例String str=Hello,World!;System.out.println(str.lastIndexOf(o,3);System.out.println(str.lastIndexOf(o,5);System.out.println(str.lastIndexOf(o,7);结果-147lengthlength()返回字符串中字符的个数实例String str=Hello,World!;System.out.println(str.

9、length();结果12regionMatchesregionMatches(int start,String other,int start,int len)比较字符串从指定位置开始与另一字符串从指定位置和指定长度的区域是否相等实例String str=Hello,World!;System.out.println(str.regionMatches(0,Hello,0,5);System.out.println(str.regionMatches(1,Hello,0,5);结果truefalsereplacereplace(char oldChar, char newChar)返回一个新

10、的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 而生成的实例String str=Hello,World!;System.out.println(str.replace(l,L);结果HeLLo,WorLd!replaceAllreplaceAll(String oldStr,String newStr)返回一个新的字符串,它是通过用oldStr替换此字符串中出现的所有newStr而生成的实例String str=Hello,World!;System.out.println(str.replaceAll(Hello,HELLO);结果HELLO,World!函

11、数名格式功能splitsplit(String str)将字符串按指定的分隔符串分离而生成的字符串数组,并返回字符串数组实例String strArr=new String2;String str=Hello,World!;strArr=str.split(,);System.out.println(strArr0);System.out.println(strArr1);结果HelloWorld!startsWithstartsWith(String str)测试是否以指定字符串开头实例String str=Hello,World!;System.out.println(str.starts

12、With(H);结果truesubstringsubstring(int beginIndex)返回指定索引处开始到此字符串末尾的子串实例String str=Hello,World!;System.out.println(str.substring(6);结果World!substringsubstring(int beginIndex, int endIndex)返回指定索引处beginIndex开始到指定索引处endIndex 1之间的子串实例String str=Hello,World!;System.out.println(str.substring(0,5);System.out.

13、println(str);结果HelloHello,World!toLowerCasetoLowerCase()将字符串中所有字符都转换为小写实例String str=Hello,World!;System.out.println(str.toLowerCase();结果hello,world!toUpperCastoUpperCase()将字符串中所有字符都转换为大写实例String str=Hello,World!;System.out.println(str.toUpperCase();结果HELLO,WORLD!trimtrim()返回去除前后空格的字符串实例String str= Hello,World! ;System.out.println(str.trim();结果Hello,World!函数名格式功能valueOfvalueOf(int i)valueOf(char ch )把不同类型的数字转化为字符串实例char ch=c,h,i,n,a;String str=Hello,World!;System.out.println(str.valueOf(123);System.out.println(str.valueOf(ch);结果123chinaap

展开阅读全文
相关资源
相关搜索

当前位置:首页 > IT计算机/网络 > 其它相关文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号