JAVA字符串处理函数汇总
发布时间:2021-05-21 07:06:25 所属栏目:大数据 来源: https://www.jb51.cc
导读: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----cont
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",1)">123456bCA System.out.println("AbCA".replaceAll("A",1)">123456bC123456 获取字符串的索引 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",1)">8 System.out.println("A123A456A789".lastIndexOf("100"));-1 查找失败 字符串提取 System.out.println("AbCA".substring(1));bCA System.out.println("AbCA".substring(1,3));bC 字符串连接 String str2="content2"content1content2content1----content2
字符串分割 String strarr[]="1*2*3*4*5-6-7".split("*|-"); for(int i=0;i<strarr.length;i++) System.out.println(strarr[i]); StringBuffer类StringBuffer sb = new StringBuffer("东小东"); sb.append("123"); System.out.println(sb);东小东123 sb.delete(0,1小东123 sb.insert(1,"dongxiaodong"小dongxiaodong东123 sb.replace(1,3,"778899"小778899ngxiaodong东123 sb.reverse(); System.out.println(sb);321东gnodoaixgn998877小 字符串类型转换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); * (编辑:北几岛) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |