zoukankan      html  css  js  c++  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

    获取字符串的索引

    复制代码
    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  //查找失败
    复制代码

    字符串提取

    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<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);
    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小
    复制代码

    字符串类型转换

    复制代码
    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);
    复制代码

    *

  • 相关阅读:
    codeforces #586 ABC~D
    codeforces #585 div2 ABCD
    编译原理实验 NFA子集法构造DFA,DFA的识别 c++11实现
    codeforces #599 div2 ABCD
    codeforces #598 div3 ABCDF
    codeforces #587 div3 ABCDE
    codeforces educational round 73 div2 ABCD
    Mud Puddles ( bfs )
    2019牛客多校第十场B.Coffee Chicken(递归)
    2019牛客多校训练第七场A. String(暴力)
  • 原文地址:https://www.cnblogs.com/qqfff/p/12359291.html
Copyright © 2011-2022 走看看