浅谈Java中String的常用方法

网友投稿 256 2023-01-13

浅谈Java中String的常用方法

String中常用的方法,我以代码的形式,来说明这些常用的方法。

@Test

public void test1(){

//1.返回字符串的长度

String s1 = "helloworld";

System.out.println(s1.length());

//2.返回某索引处的字符

System.out.println(s1.charAt(1));

//3.判断字符串是否是空字符串

System.out.println(s1.isEmpty());

//4.将String中的所有字符串转换成小写

String s2 = "ShoPPing";

String s3 = s2.toLowerCase();

System.out.println(s3);

//5.将String中的所有字符串转换成大写

String s4 = s2.toUpperCase();

System.out.println(s4);

//6.返回字符串的副本,忽略前导空白和尾部空白

String s5 = " An dro id ";

String s6 = s5.trim();

System.out.println("**********"+s5+"**********");

System.out.println("**********"+s6+"**********");

//7.比较字符串的内容是否相同

System.out.println(s1.equals(s5));

//8.与equals方法类似,njrIb这个忽略大小写

String s7="abcDef";

String s8="ABCDef";

System.out.println(s7.equals(s8));//false

System.out.println(s7.equalsIgnoreCase(s8));//true

//9.将指定字符串连接到此字符串的结尾,等价于"+"

String s9="abc";

String s10 = s9.concat("def");

System.out.println(s10);

//10.比较两个字符串的大小

String s11="abe";

System.out.println(s9.compareTo(s11)); //-2 说明s9小于s11

//11.返回一个新的字符串,它是此字符串的从bedinIndex开始截取到最后的一个子字符串

String s12 = "我一定要学会android";

System.out.println(s12.substring(6));

//12.返回一个新字符串,它是此字符串从beginIndex开始截取到endIndex(不包括)的一个子字符串

String s13 = s12.substring(2, 6);

System.out.println(s13);

}

输出结果如下:

当然String中,不止这些方法,只不过这些是比较常用的方法。

下面再说一些其他的方法:

还是以代码为例:

@Test

public void test2(){

//1.测试此字符串是否以指定的后缀结束

String s1 = "helloworld";

System.out.println(s1.endsWith("ld"));//true

//2.测试此字符串是否以指定的前缀结束

System.out.println(s1.startsWith("hel"));//true

//3.测试此字符串从指定索引开始的字符串是否以指定前缀开始

System.out.println(s1.startsWith("ow", 4));//true

//4.当且仅当此字符串包含指定的char值序列时,返回true;

System.out.println(s1.contains("lo"));//true

System.out.println(s1.contains("lowld"));//false

//5.返回指定子字符串在此字符串中第一次出现处的索引

System.out.println(s1.indexOf("el"));//1

//6.返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始

System.out.println(s1.indexOf("ow",3));//4

//7.返回指定子字符串在此字符串中最右边出现处的索引

njrIb System.out.println(s1.lastIndexOf("o"));//6

//8.返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索

System.out.println(s1.lastIndexOf("o", 5));//4

}

下面是String中关于正则的方法:

@Test

public void test3(){

//1.返回一个新的字符串,它是通过用newChar替换此字符串中出现的所有oldChar得到的

String s1="你好,我是程序员小白,小白!";

System.out.println(s1.replace('小','大'));

//2.使用指定的字面值替换序列,替换此字符串所有匹配字面值目标序列的子字符串

System.out.println(s1.replace("小白","大牛"));

//3.使用给定的replacement替换此字符串所有匹配给定的正则表达式的子字符串

String s2="12Hello2342World234Android";

String s3 = s2.replaceAll("\\d+", ",").replaceAll("^,|,$", "");

System.out.println(s3);

//4.告知此字符串是否匹配给定的正则表达式

String s4=http://"123456";

//判断s4字符串中是否全部由数字组成,即1-n个数字组成

boolean matches = s4.matches("\\d+");

System.out.println(matches);

String tel="0373-12345678";

//判断这是否是河南的一个固定电话

boolean matches1 = tel.matches("0373-\\d{7,8}");

System.out.println(matches1);

//5.根据给定正则表达式的匹配拆分此字符串

String s5="hello|world|android";

String[] split = s5.split("\\|");

for (int i = 0; i < split.length; i++) http://{

System.out.println(split[i]);

}

System.out.println("****************************");

//6.根据匹配给定的正则表达式来拆分此字符串,最多不能超过limit个,如果超过了,剩下的都全部放到最后一个元素中

String s6="hello.world.android";

String[] split1 = s6.split("\\.");

for (int i = 0; i < split1.length; i++) {

System.out.println(split1[i]);

}

}

输出结果如下:

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring中的注解之@Override和@Autowired
下一篇:SpringBoot整合Redisson的步骤(单机版)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~