java字符串的处理函数,JAVA字符串处理函数汇总

String类

String str1="content1";

简单的字符串拼接

String str2="content2";

String str3=str1+"----"+str2;

System.out.println(str3);//content1----content2

System.out.println(str1.concat(str2).concat(str3));//content1content2content1----content2

字符串长度

System.out.print(str1.length());//8

System.out.print("东小东".length());//3

将字符串分割为字符数组

System.out.print(str1.toCharArray()[0]);//c

System.out.print("东小东".toCharArray()[0]);//东

获取指定位置的字符

System.out.print(str1.charAt(0));//c

System.out.print("东小东".charAt(0));//东

去掉字符串的左右空格

System.out.print(" 东小东 ".trim());//东小东

全部转换为大小写

System.out.print("AbCd".toUpperCase());//ABCD

System.out.print("AbCd".toLowerCase());//abcd

判断字符串是否以某个字符开头或者结尾

System.out.println("AbCd".startsWith("A"));//true

System.out.println("AbCd".endsWith("A"));//false

System.out.println("AbCd".startsWith("C",2));//true//指定开始位置

字符串替换

System.out.println("AbCdA".replace(‘A‘,‘8‘));//8bCd8

System.out.println("AbCd".replace("A","123456"));//123456bCd

System.out.println("AbCA".replaceFirst("A","123456"));//123456bCA

System.out.println("AbCA".replaceAll("A","123456"));//123456bC123456

获取字符串的索引

fcecaa27ea5212ceb9bf034c36bfbf34.gif

System.out.println("A123A456A789".indexOf(‘A‘));//0

System.out.println("A123A456A789".indexOf(‘A‘,1));//4

System.out.println("A123A456A789".indexOf("A"));//0

System.out.println("A123A456A789".indexOf("A",1));//4

System.out.println("A123A456A789".lastIndexOf(‘A‘));//8

System.out.println("A123A456A789".lastIndexOf(‘A‘,8));//8 //从指定索引反向搜索

System.out.println("A123A456A789".lastIndexOf("A"));//8

System.out.println("A123A456A789".lastIndexOf("A",8));//8

System.out.println("A123A456A789".lastIndexOf("100"));//-1 //查找失败

fcecaa27ea5212ceb9bf034c36bfbf34.gif

字符串提取

System.out.println("AbCA".substring(1));//bCA

System.out.println("AbCA".substring(1,3));//bC

字符串连接

String str2="content2";

String str3=str1+"----"+str2;

System.out.println(str3);//content1----content2

System.out.println(str1.concat(str2).concat(str3));//content1content2content1----content2

字符串分割

split() 方法根据匹配给定的正则表达式来拆分字符串。

注意: . 、 $、 | 和 * 等转义字符,必须得加 \\。

注意:多个分隔符,可以用 | 作为连字符。

String strarr[]="1*2*3*4*5-6-7".split("\\*|-");

for(int i=0;i

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

StringBuffer类

fcecaa27ea5212ceb9bf034c36bfbf34.gif

StringBuffer sb = new StringBuffer("东小东");

sb.append("123");

System.out.println(sb);//东小东123

sb.delete(0, 1);

System.out.println(sb);//小东123

sb.insert(1, "dongxiaodong");

System.out.println(sb);//小dongxiaodong东123

sb.replace(1, 3, "778899");

System.out.println(sb);//小778899ngxiaodong东123

sb.reverse();

System.out.println(sb);//321东gnodoaixgn998877小

fcecaa27ea5212ceb9bf034c36bfbf34.gif

字符串类型转换

fcecaa27ea5212ceb9bf034c36bfbf34.gif

String value= "12.3";

byte bx = Byte.parseByte(value);

short tx = Short.parseShort(value);

int ix = Integer.parseInt(value);

long lx = Long.parseLong(value );

Float fx = Float.parseFloat(value);

fcecaa27ea5212ceb9bf034c36bfbf34.gif

*

原文:https://www.cnblogs.com/qqfff/p/12359291.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值