String类的常用方法

字符串与字符数组:
public String(char[] value) //将字符数组中的所有内容变为字符串 String s = new String(Arrar);
public String(char[] value, int offset, int count) //将部分字符数组中的内容变为字符串。
public char charAt(int index) //取得索引位置的指定字符(索引从0开始)。
public char[] toCharArray() //将字符串变为字符数组。

字符串比较:
public boolean equals(Object anObject)
public int compareTo(String anotherString)

字符串查找:
public boolean contains(String s) //Returns true if and only if this string contains the specified sequence of char values.
public int indexOf(String str)
public int indexOf(String str, int fromIndex) //从指定位置开始查找并返回子字符串的位置。
public int lastIndexOf(String str) //由后向前查找某字符串位置。
public int lastindexOf(String str, int fromIndex)
public boolean startsWith(String prefix) //判断是否以指定字符串开头。
public boolean startsWith(String prefix, int toffset ) //从指定位置开始判断
public boolean endsWith(String suffix) //判断是否以指定字符串结尾。

字符串替换:
public String replace(CharSequence target, CharSequence replacement)
public String replaceAll(String regex, String replacement) //替换所有内容
public String replaceFirst(String regex, String replacement) //替换首个内容

字符串拆分:
public String[] split(String regex) //将字符串整个拆分。
public String[] split(String regex, int limit) //将字符串部分拆分。limit为拆分后的字符串数组最多个数。
有符号不能被直接拆分:如 . |等,此时需要在之前加上\\。例如str.split("\\.")。

字符串截取:
public String substring(int beginIndex) 从指定位置截取到结尾。
public String substring(int beginIndex, int endIndex) //从指定位置截取到指定位置。

字符串其它操作方法:
public String trim() //去除字符串左右的空格,保留中间空格。
public String toUpperCase() //字符串转大写。
public String toLowerCase() //字符串转小写。
int length()
public boolean isEmpty() //判断是否为空字符串,但不是null,是长度为0。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值