java字符串类常用方法

一 String类常用方法
String s1 = “320130199501244550”;
System.out.println(s1);
//字符串长度
System.out.println(s1.length());
//获取字符串中第3个字符
System.out.println(s1.charAt(2));
//拼接:字符串拼接字符串
System.out.println(s1.concat(“abcd”));
//替换:字符串中指定的某个字符
System.out.println(s1.replace(‘3’, ''));
//替换:字符串中指定的某个子字符串
System.out.println(s1.replaceAll(“1995”,"
***"));
//截取:子字符串,范围 [beginIndex, 最后一个字符]
System.out.println(s1.substring(10));
//截取:子字符串,范围 [beginIndex, endIndex)
System.out.println(s1.substring(10,13));
//查询:字符串是否为空字符串
System.out.println(s1.isEmpty());
//查询:字符串是否以某个子字符串开头
System.out.println(s1.startsWith(“320130”));
//查询:字符串是否以某个子字符串结尾
System.out.println(s1.endsWith(“4550”));
//查询:某个子字符串在字符串中首次出现的位置,返回-1表示未找到
System.out.println(s1.indexOf(“1995”));
//查询:某个子字符串在字符串中最后一次出现的位置,返回-1表示未找到
System.out.println(s1.lastIndexOf(“1995”));
//比较:比较两个字符串的内容是否相同
System.out.println(s1.equals(“320130199501244550”));
//比较:比较两个字符串的内容大小。返回正数表示s1大,返回负数表示实参大,返回0表示两个字符串内容相同
System.out.println(s1.compareTo(“320130199501244550”));
//分割:字符串按照指定的 子字符串(支持正则) 进行分割(不限制分割数量),返回字符串数组String[]
System.out.println(Arrays.toString(s1.split(“1”)));
//分割:字符串按照指定的 子字符串(支持正则) 进行分割(限制分割数量2, 超过的话最后一个包含剩余的子串),返回字符串数组String[]
System.out.println(Arrays.toString(s1.split(“1”,2)));

二 StringBuffer/StringBuilder类常用方法
StringBuffer s1 = new StringBuffer(“320130199501244550”);
System.out.println(s1);
//字符串长度
System.out.println(s1.length());
//获取字符串中第3个字符
System.out.println(s1.charAt(2));
//追加:字符串追加字符串
System.out.println(s1.append(“abcd”));
//替换:字符串中指定的某一段字符,范围[beginIndex, endIndex)
System.out.println(s1.replace(0, 2, “str”));
//截取:子字符串,范围 [beginIndex, 最后一个字符]
System.out.println(s1.substring(10));
//截取:子字符串,范围 [beginIndex, endIndex)
System.out.println(s1.substring(10,13));
//查询:字符串是否为空字符串
System.out.println(s1.length()==0);
//查询:某个子字符串在字符串中首次出现的位置,返回-1表示未找到
System.out.println(s1.indexOf(“1995”));
//查询:某个子字符串在字符串中最后一次出现的位置,返回-1表示未找到
System.out.println(s1.lastIndexOf(“1995”));
//比较:比较两个字符串的内容是否相同
System.out.println(s1.equals(“320130199501244550”));
//比较:比较两个字符串的内容大小。返回正数表示s1大,返回负数表示实参大,返回0表示两个字符串内容相同
System.out.println(s1.compareTo(new StringBuffer(“320130199501244550”)));

三 String/StringBuffer/StringBuilder三者之间的转换
String s1 = new String(StringBuffer/StringBuilder对象);
StringBuffer s2 = new String(String/StringBuilder对象);
StringBuilder s3 = new String(String/StringBuffer对象);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值