String类常用属性方法小结

1.     字符串替换

replaceAll(String old,String new)//将字符串中的old字符,替换为new字符。

例如:

<span style="font-size:18px;">String a=”hello world”;
String b=a.replaceAll(“o”,”a”);</span>

最后b的输出结果为“hella warld”


2.     判断是否以指定的字符串开头或结尾

      Str.startsWith(String start)

      Str.endsWith(String end)

例如:

<span style="font-size:18px;">String str1=”aahello world”;
String str2=”hello worldaa”;
If(str1.startsWith(“aa”)){
          System.out.println(str1);
}</span>

3.     大小写转换

      ①小写变大写:str.toUpperCase();

      ②大写变小写:str.toLowerCase();


4.     拆分字符串

       Str.split(“**”)

例如:

<span style="font-size:18px;">String str=”hello”;
String st[]=str.split(“,”);
For(int i=0;i<st.length;i++){
System.out.println(st[i]);
}</span>

最终st[]的输出结果为:h,e,l,l,o,


5.     字符串截取

      ①  从指定位置开始一直截取到字符串的最后:str.substring(int begin)

      ②  截取指定范围的字符串:str.substring(intbegin,int end)


6.     查找指定的字符串是否存在

      ①从头开始查找,查到之后返回所在位置:str.indexOf(String s)

      ②从指定位置开始查找,查到之后返回所在位置:str.indexOf(String s,int begin)

例如:

<span style="font-size:18px;">String str=”hello”;
Int a=str.indexOf(“o”);//从头开始查找字符o,最后a的结果为4.
Int b=str.indexOf(“o”,2);//从第2个位置开始查找o,最后a的结果为4.</span>

7.     取一个字符串的长度

      Str.length


8.     从字符串中取出指定位置的字符

      Str.charAt(int a)

例如:

<span style="font-size:18px;">String str=”hello”;
Str.charAt(3);//取出字符串中第4个字符</span>

9.     移除字符串中指定的字符

      ①字符串的开头:str.TrimStart()

      ②字符串的末尾:str.TrimEnd()

      ③字符串的开头和末尾去除空格:str.trim();

例如:

<span style="font-size:18px;">char[] a={“h”,”o”};
String b=”hello”;
String c=b.trimstart(a);
String d=b.trimend(a);
C=”ello“;
d=”hell”;</span>

10.  删除指定位置的字符

      Str.remove(int begin,int end);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值