java常见字符串函数 (1)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
常见字符串函数
函数名格式功能
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("!")); 结果
true
System.out.println(str.endsWith("d")); 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!";
System.out.println(str.indexOf('o')); 结
果 4
indexOf indexOf(int ch, int fromIndex) 从指定的索引开
始从前往后搜索,
返回在此字符串
中第一次出现指
定字符处的索引
实例String str="Hello,World!";
System.out.println(str.indexOf('o',6)); 结
果7
函数名格式功能
lastIndexOf lastIndexOf(int ch) 返回最后一次出现
的指定字符在此字
符串中的索引
实例String str="Hello,World!";
System.out.println(stIndexOf('o')); 结
果7
lastIndexOf lastIndexOf(int ch,int fromIndex) 从指定的索引处开
始进行从后向前搜
索,返回最后一次
出现的指定字符在
此字符串中的索引
实例String str="Hello,World!";
System.out.println(stIndexOf('o',3));
System.out.println(stIndexOf('o',5));
System.out.println(stIndexOf('o',7)); 结
果-1
4
7
length length() 返回字符串中字符
的个数
实例String str="Hello,World!";
System.out.println(str.length()); 结
果12
regionMatches regionMatches(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)); 结
果true
false
replace replace(char oldChar, char newChar) 返回一个新的字符
串,它是通过用
newChar 替换此字
符串中出现的所有
oldChar 而生成的
实例String str="Hello,World!";
System.out.println(str.replace('l','L')); 结
果HeLLo,WorLd!
replaceAll replaceAll(String oldStr,String newStr) 返回一个新的字符
串,它是通过用
oldStr替换此字符
串中出现的所有
newStr而生成的
实例String str="Hello,World!";
System.out.println(str.replaceAll("Hello","HELLO")); 结
果HELLO,Worl
d!
函数名格式功能
split split(String str) 将字符串按指定的
分隔符串分离而生
成的字符串数组,
并返回字符串数组
实例String[] strArr=new String[2];
String str="Hello,World!";
strArr=str.split(",");
System.out.println(strArr[0]);
System.out.println(strArr[1]); 结
果
Hello
World!
startsWith startsWith(String str) 测试是否以指定字
符串开头
实例String str="Hello,World!";
System.out.println(str.startsWith("H")); 结
果true
substring substring(int beginIndex) 返回指定索引处开
始到此字符串末尾
的子串
实例String str="Hello,World!";
System.out.println(str.substring(6)); 结
果World!
substring substring(int beginIndex, int endIndex) 返回指定索引处
beginIndex开始到
指定索引处
endIndex – 1之间的
子串
实例String str="Hello,World!";
System.out.println(str.substring(0,5));
System.out.println(str); 结
果Hello
Hello,World!
toLowerCase toLowerCase() 将字符串中所有字
符都转换为小写
实例String str="Hello,World!";
System.out.println(str.toLowerCase()); 结
果hello,world!
toUpperCas toUpperCase() 将字符串中所有字
符都转换为大写
实例String str="Hello,World!";
System.out.println(str.toUpperCase()); 结
果HELLO,WOR
LD!
trim trim() 返回去除前后空格
的字符串
实例String str=" Hello,World! ";
System.out.println(str.trim()); 结
果Hello,World!
函数名格式功能
valueOf valueOf(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)); 结
果
123
china
append append(String str) 将指定的字符串str追
加到原字符串中,原字
符串内容将发生变化
实例StringBuffer str=new StringBuffer("Welcome to ");
str.append("china");
System.out.println(str); 结
果
Welcome to china
delete delete(int start,int end) 删除字符串中从指定
开始位置到结束位置-1
之间的字符
实例StringBuffer str=new StringBuffer("Welcome to ");
str.delete(0,3);
System.out.println(str); 结
果
come to
deleteCharAt deleteCharAt(int index) 删除字符串中指定位
置的字符
实例StringBuffer str=new StringBuffer("Welcome to ");
str.deleteCharAt(0);
System.out.println(str); 结
果
elcome to
insert insert(int offset, String str) 将str插入到字符串指
定的位置之前
实例StringBuffer str=new StringBuffer("Welcome to ");
str.insert(0,"china ");
System.out.println(str); 结
果
china Welcome to
setCharAt setCharAt(int index,char ch) 将给定索引处index 的
字符设置为ch
实例StringBuffer str=new StringBuffer("Welcome to ");
str.setCharAt(1,'E');
str.setCharAt(6,'E');
System.out.println(str); 结
果
WElcomE to
setLength setLength(int len) 设置字符串的长度
实例StringBuffer str=new StringBuffer("Welcome to ");
System.out.println(str.length());
str.setLength(20);
System.out.println(str.length()); 结
果11
20
reverse reverse()颠倒字符串的所有
字符
实例StringBuffer str=new StringBuffer("Welcome to ");
System.out.println(str);
str.reverse();
System.out.println(str); 结
果Welcome to
ot emocleW。