String类常用方法大全

一.字符串的替换

 1.replace方法

String string1="Hello Word";     

String result1=string1.replace("Hello","你好");      

String result2=string1.replace("abc","你好");

Log.d("TAG","result1----:"+result1);//你好 Word(替换的目标存在)

Log.d("TAG","result2----:"+result2);//Hello Word(替换的目标不存在)

2.replaceAll方法

String string1="Hello Word";

String result3=string1.replaceAll("o","A");

String result4=string1.replaceAll("111","A");

Log.d("TAG","result3----:"+result3);//HellA WArd(替换的目标存在)

Log.d("TAG","result4----:"+result4);//Hello Word(替换的目标不存在)

3.replaceFirst方法

String string1="Hello Word";

String result5=string1.replaceFirst("H","A");

String result6=string1.replaceFirst("o","A");

String result7=string1.replaceFirst("111","A");

Log.d("TAG","result5----:"+result5);//Aello Word(替换的目标存在)

Log.d("TAG","result6----:"+result6);//HellA Word(替换的目标存在)

Log.d("TAG","result7----:"+result7);//Hello Word(替换的目标不存在)

二.字符串的拆分

1.split(String str)方法

 

String string1 = "abc1def1ghi1asd12345";
String result1[] = string1.split("1");
for (String s1 : result1) {
    Log.d("TAG", "s1----:" + s1);
}


String result2[] = string1.split("L");
for (String s2 : result2) {
    Log.d("TAG", "s2----:" + s2);
}
s1----:abc
s1----:def
s1----:ghi
s1----:asd
s1----:2345


s2----:abc1def1ghi1asd12345

2.split(String str,int limit)方法

limit 参数控制模式应用的次数,因此影响结果数组的长度。如果该限制 n 大于 0,则模式将被最多应用 n - 1 次,数组的长度将不会大于 n,而且数组的最后项将包含超出最后匹配的定界符的所有输入。 如果 n 为非正,则模式将被应用尽可能多的次数,而且数组可以是任意长度。如果 n 为零,则模式将被应用尽可能多的次数,数组可有任何长度,并且结尾空字符串将被丢弃。
 

String line = "aa,bb,cc,dd,,,,";
String result1[] = line.split(",");
for (String s1 : result1) {
    Log.d("TAG", "s1----:" + s1);
}



String result2[] = line.split(",", line.length());
for (String s2 : result2) {
    Log.d("TAG", "s2----:" + s2);
}

s1----:aa
s1----:bb
s1----:cc
s1----:dd


s2----:aa
s2----:bb
s2----:cc
s2----:dd
s2----:
s2----:

三.字符串截取

1.substring(int startIndex)方法

String string1="1234567";
String result1=string1.substring(2);//[2,最后]
String result2=string1.substring(555);//报错
Log.d("TAG","result1----:"+result1);//34567

2.substring(int startIndex,int endIndex)方法

String string1="1234567";
String result3=string1.substring(2,4);//[2,4)
String result4=string1.substring(2,555);//报错
Log.d("TAG","result3----:"+result3);//34

四.字符串的查找

1.contains方法

String string="abcd";
boolean result1=string.contains("a");
boolean result2=string.contains("LLL");
Log.d("TAG","result1----:"+result1);//true
Log.d("TAG","result2----:"+result2);//false

2.endsWith方法

String string="abcd";
boolean result1=string.endsWith("d");
boolean result2=string.endsWith("LLL");
Log.d("TAG","result1----:"+result1);//true
Log.d("TAG","result2----:"+result2);//false

3.startsWith(String str)方法

 

String string="abcd";
boolean result1=string.startsWith("a");
boolean result2=string.startsWith("LLL");
Log.d("TAG","result1----:"+result1);//true
Log.d("TAG","result2----:"+result2);//false

4.startsWith(String str,int offIndex)方法

String string="abcd";
boolean result3=string.startsWith("a",0);
boolean result4=string.startsWith("a",222222);
boolean result5=string.startsWith("LLL",2);
boolean result6=string.startsWith("LLL",222222);
Log.d("TAG","result3----:"+result3);//true
Log.d("TAG","result4----:"+result4);//false
Log.d("TAG","result5----:"+result5);//false
Log.d("TAG","result6----:"+result6);//false


 

5.indexOf(String str)

String string="abcdadvccccaaa";
int result1=string.indexOf("a");
int result2=string.indexOf("c");
int result3=string.indexOf("NN");
Log.d("TAG","result1----:"+result1);//0
Log.d("TAG","result2----:"+result2);//2
Log.d("TAG","result3----:"+result3);//-1:没找到

6.indexOf(String str,int formIndex)方法

String string="abcdadvccccaaa";
int result4=string.indexOf("a",2);
int result5=string.indexOf("a",222222);
int result6=string.indexOf("NN",2);
int result7=string.indexOf("NN",222222);
Log.d("TAG","result4----:"+result4);//4
Log.d("TAG","result5----:"+result5);//-1:没找到
Log.d("TAG","result6----:"+result6);//-1
Log.d("TAG","result7----:"+result7);//-1

7.lastIndexOf(String str)方法

String string="abcdadvccccaaa";
int result1=string.lastIndexOf("a");
int result2=string.lastIndexOf("c");
int result3=string.lastIndexOf("NN");
Log.d("TAG","result1----:"+result1);//13
Log.d("TAG","result2----:"+result2);//10
Log.d("TAG","result3----:"+result3);//-1:没找到

8.lastIndexOf(String str,int formIndex)方法

String string="abcdadvccccaaa";
int result4=string.lastIndexOf("a",5);
int result5=string.lastIndexOf("a",222222);
int result6=string.lastIndexOf("NN",2);
int result7=string.lastIndexOf("NN",222222);
int result8=string.lastIndexOf("d",2);
Log.d("TAG","result4----:"+result4);//4
Log.d("TAG","result5----:"+result5);//13
Log.d("TAG","result6----:"+result6);//-1:没找到
Log.d("TAG","result7----:"+result7);//-1
Log.d("TAG","result8----:"+result8);//-1

9.charAt(int index)方法

String string="abcdefasssbbcca";
char result1=string.charAt(0);
char result2=string.charAt(4);
char result3=string.charAt(11111111);//报错
Log.d("TAG","result1----:"+result1);//a
Log.d("TAG","result2----:"+result2);//e
Log.d("TAG","result3----:"+result3);//报错

 

五.字符串比较

1.equals(Object o)方法

String string1="abcDa";
String string2="abcda";
boolean result1=string1.equals(string2);
Log.d("TAG","result1----:"+result1);//false

2.equalsIgnoreCase(Object o)方法

String string1="abcDa";
String string2="abcda";
boolean result2=string1.equalsIgnoreCase(string2);
Log.d("TAG","result2----:"+result2);//true

3.compareTo(String str)方法

String string1="a";
String string2="A";
String string3="bb";
String string4="a";
int result1=string1.compareTo(string2);
int result2=string1.compareTo(string4);
int result3=string1.compareTo(string3);
Log.d("TAG","result1----:"+result1);//32
Log.d("TAG","result2----:"+result2);//0
Log.d("TAG","result3----:"+result3);//-1

 

4.compareToIgnoreCase(String str)方法

String string1="a";
String string2="A";
String string3="bb";
String string4="a";       
int result4=string1.compareToIgnoreCase(string2);
int result5=string1.compareToIgnoreCase(string4);
int result6=string1.compareToIgnoreCase(string3);       
Log.d("TAG","result4----:"+result4);//0
Log.d("TAG","result5----:"+result5);//0
Log.d("TAG","result6----:"+result6);//-1



在compareTo上会返回的数据类型为int型,而对于int型有如下三种的返回
大于:>0
小于:<0
等于:=0
compareTo对于大小的比较就是字母编码的比较

六.字符串转数组

1.将字符串转换为字节数组

2.将字符串变为字符数组

七.其他方法

1.字符串长度

2.大写转小写

3.小写转大写

4.去掉前后空格(中间保留)

5.字符串连接(一般用+即可)

6.将内容保存到对象池之中

7.判断是否为空字符串,但是不是null

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值