String类的归纳总结

字符串和字节数组的相互转换:

字符串->字节数组

str.getBytes()

//根据指定的字符编码获得字节数组

str.getBytes(Charset charset)

str.getBytes(String charsetName)


字节数组->字符串

new String(Byte[] bbuf)

new String(Byte[] buf, int offSet, int len)//使用缓冲区特定的子数组创建字符串


new String(Byte[] buf, String chatSet)//按指定的编码创建字符串

new String(Byte[] buf, int offSet, int len, String charSet)//指定编码



字符串和字符数组的相互转换

字符数组->字符串

new String(char[] ch)

new String(char[] ch, int offSet, int len)


//字符串->字符数组

toCharArray();

getChars(int srcBegin, int srcEnd, char[] des, int desBegin)//复制子串到字符数组

String.copyValueOf(char[] data)

String.copyValueOf(char[] data, int offSet, int len)





字符串之间的比较

compareTo(String anotherString)//区分大小写

compareToIgnoreCase(String anotherString)//不区分大小写


字符串连接

concat(String str)//习惯上使用"+"比较常见



字符串的判断

contains(String subString)//是否包含subString这个子串

length()字符串的长度

isEmpty()//当字符串的长度是0时返回true


matches(String regex)//字符串和正则表达式是否匹配


equals(Object obj)//字符串的值是否相同

equalsIgnoreCase(String str)//不区分大小写比较两个字符串


//判断字符串的开头是否是prefix

startsWith(String prefix)

startsWith(String prefic, int offset)


endsWith(String suffix)//字符串的后缀是否是suffix


//比较字符串的区域是否相等

regionMatches(boolean Ignore, int toffSet, String other, int offset, int len)//

regionMatches(int toffet, String other, int offset, int len)


搜索字符或者字符串

//从小标0开始找

indexOf(int ch)

indexOf(String str)


//从下标offset开始查找

index(intr ch, int offset)

indexOf(String str, int offset)


//从字符串的末尾往前查找

lastIndexOf(int ch)

lastIndexOf(String str);


//从下标fromIndex开始往前查找

lastIndexOf(int ch, int fromIndex)

lastIndexOf(String str, int fromIndex)



取得子串

subString(int BeginIndex)

subString(int BeginIndex, int EndIndex)




替换字符串的内容

replace(char oldChar, char newChar) //替换一个字符

replace(CharSequence target, CharSequence replacement)//替换一个字符串

    replaceFirst(String regex, String replacement)//替换第一个匹配正则的字符串

    replaceAll(String regex, String replacement)//替换所有匹配正则的字符串


字符串切割

split(String regex)//按正则表达式切割,返回一个字符串数组

split(String regex, int limit) //只切割limit-1次,最后得到的字符串数组长度是limit


大小写转换

toUpperCase()

toLowerCase90


去除两端的空格

trim()


基本数据转换为字符串

String.valueOf()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值