java中String类中方法的使用

String s1 = "   A  b  c  ";
String s2 = "def";
System.out.println(s1+"\n"+s2);
//Concat(String str) 连接连个字符串
System.out.println("----------Concat-------------");
System.out.println(s1.concat(s2));
System.out.println("-----------contains------------");
//boolean contains(CharSequence s)判断字符串中是否包含指定字符
System.out.println(s1.contains("a"));//true
System.out.println("-----------endsWith------------");
//boolean endsWith(String suffix)判断字符串是否以指定的字符串结尾
System.out.println(s1.endsWith("a"));//false
System.out.println("-----------startsWith-----------");
//boolean startsWith(String prefix)判断字符串是否以指定的字符串开头
System.out.println(s1.startsWith("a"));//true
System.out.println("-----------indexOf------------");
//int indexOf(String str)从前往后在字符串中找指定字符串的索引,找不到返回-1
System.out.println(s1.indexOf("b"));//1
System.out.println("------------lastIndexOf-----------");
//int lastInterOf(String str)从后往前在字符串中找指定字符串的索引,找不到返回-1
System.out.println(s2.lastIndexOf("a"));//-1
System.out.println("------------replace-----------");
//String replace(CharSequence target,CharSequence replacement)
//把字符串中所有的目标字符串,替换为新的字符串
System.out.println(s1.replace(" ", ""));
System.out.println("-----------substring------------");
//String substring(int beginIndex,int endIndex)从开始索引截取字符串到
//结束索引截取字符串;包含开始索引不包含结束索引
System.out.println(s1.substring(0, 2));
System.out.println("-----------toCharArray------------");
//char[] toCharArray() 将此字符串转换为一个新的字符数组。
System.out.println(Arrays.toString(s1.toCharArray()));
System.out.println("------------getBytes-----------");
//byte[] getBytes() 查询系统默认的编码吧字符串转换为字节数组
System.out.println(Arrays.toString(s1.getBytes()));
System.out.println("------------toLowerCase-----------");
//String toLowerCase() 把字符串中的英文字母全部转换为小写
System.out.println(s1.toLowerCase());
System.out.println("------------toUpperCase-----------");
//String toUpperCase() 把字符串中的英文字母全部转换为大写
System.out.println(s2.toUpperCase());
System.out.println("----------trim-------------");
//String trim() 去掉字符串两端的空格
System.out.println(s1.trim());
System.out.println("------------split-----------");
//String[] split(String regex) 根据自定的字符串对字符串进行切割遇到.要用\\.来表示
String s3 = "hello.java";
System.out.println(Arrays.toString(s3.split("\\.")));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值