Java 字符串和字符串缓冲区的常用方法

Java 字符串和字符串缓冲区的常用方法

字符串 String

  • 具有判断功能的方法

    • 示例

      // equals 判断两个字符串是否具有相同的字符序列
      String str = "abcdef";
      
      boolean result1 = str.equals("abcdef");
      boolean result2 = str.equals("ABCdef");
      
      System.out.println("(equals) : result1 = " + result1 + ", result2 = "
              + result2);
      
      // equalsIgnoreCase 与equals相同,但忽略大小写
      result1 = str.equalsIgnoreCase("abcdef");
      result2 = str.equalsIgnoreCase("ABCdef");
      
      System.out.println("(equalsIgnoreCase) : result1 = " + result1
              + ", result2 = " + result2);
      
      // 判断是否包含指定的字符串
      result1 = str.contains("a");
      result2 = str.contains("x");
      System.out.println("(contains) : result1 = " + result1 + ", result2 = "
              + result2);
      
      // startsWith 判断是否以指定字符串开始
      result1 = str.startsWith("abc");
      result2 = str.startsWith("def");
      System.out.println("(startsWith) : result1 = " + result1
              + ", result2 = " + result2);
      
      // endsWith 判断是否以指定字符串结束
      result1 = str.endsWith("abc");
      result2 = str.endsWith("def");
      System.out.println("(endsWith) : result1 = " + result1 + ", result2 = "
              + result2);
      
      // isEmpty 判断字符串是否为空
      result1 = str.isEmpty();
      result2 = "".isEmpty();
      System.out.println("(isEmpty) : result1 = " + result1 + ", result2 = "
              + result2);
      
      // compareTo 根据字典顺序,判断两个字符串每个字符的Unicode值
      // 按照字典顺序,比指定字符串大,返回正数,小则返回负数,相等返回0
      int num1 = str.compareTo("abcdef");
      int num2 = str.compareTo("abcdee");
      int num3 = str.compareTo("abcdeg");
      System.out.println("(compareTo) : num1 = " + num1 + ", num2 = " + num2
              + ", num3 = " + num3);
      
      // compareToIgnoreCase 忽略大小写,与compareTo相同
      num1 = str.compareToIgnoreCase("ABCdef");
      System.out.println("(compareToIgnoreCase) : num1 = " + num1);
    • 运行结果
      Java 字符串和字符串缓冲区的常用方法_第1张图片

  • 具有获取功能的方法

    • 演示

      // length 获得字符串长度
      String str = "abcdef";
      
      System.out.println("(length) : " + str.length());
      
      // charAt 获得指定位置的字符
      System.out.println("(charAt) : " + str.charAt(2));
      
      // indexOf 获得指定字符串第一次出现的位置,没有找到返回 -1
      System.out.println("(indexOf) : " + str.indexOf("cd"));
      
      // lastIndexOf 获得指定字符串最后一次出现的位置,没有找到返回 -1
      System.out.println("(lastIndexOf) : " + str.lastIndexOf("de"));
      
      // split 根据指定的正则表达式,切割字符串,返回字符串数组
      String[] ss = "abc|def|ghi".split("[|]");
      System.out.println("(split) : " + Arrays.toString(ss));
      
      // substring 截取字符串
      System.out.println("(substring) : " + str.substring(2)); // 从索引2的位置截取,截取到最后
      System.out.println("(substring) : " + str.substring(2, 4));// 从索引2的位置截取,截取到索引4,不包括4(包头不包尾)
    • 运行结果
      Java 字符串和字符串缓冲区的常用方法_第2张图片

  • 具有转换功能的方法

    • 示例

      // getBytes 将字符串转换成字节数组
      String str = "abcdef";
      byte[] byteBuf = str.getBytes();
      System.out.println("(getBytes) : " + Arrays.toString(byteBuf));
      
      // toCharArray 将字符串转换成字符数组
      char[] charBuf = str.toCharArray();
      System.out.println("(toCharArray) : " + Arrays.toString(charBuf));
      
      // valueOf 将任意值/对象转换成字符串
      System.out.println("(valueOf) : "
              + String.valueOf(new char[] { 97, 98, 99 }));
      
      // toUpperCase 将字符转换成大写形式
      System.out.println("(toUpperCase) : " + str.toUpperCase());
      
      // toLowerCase 将字符串转换成小写形式
      System.out.println("(toLowerCase) : " + "ABCdef".toLowerCase());
      
      // concat 拼接两个字符串
      str = "aaa".concat("ccc");
      System.out.println("(concat) : " + str);
    • 运行结果
      Java 字符串和字符串缓冲区的常用方法_第3张图片

  • 具有替换功能的方法

    • 示例

      // replace 把字符串中指定字符替换成新的字符串
      String str = " \t  abc abc abc   ";
      System.out.println("(replace) : " + str.replace("a", "x"));
      
      // trim 去除字符串两端的空白符
      System.out.println("(trim) : " + str.trim());
    • 运行结果
      运行结果

字符串缓冲区 StringBuffer/StringBuilder

  • 常用方法

    • 示例

      StringBuffer sb = new StringBuffer();
      
      // 获得 StringBuffer 缓冲区的初始长度
      System.out.println("(capacity) : " + sb.capacity());
      
      // append 追加字符串
      sb.append("abc");
      System.out.println("(append) : " + sb);
      
      // length 获得缓冲区的实际长度
      System.out.println("(length) : " + sb.length());
      
      // insert 插入数据到缓冲区指定的位置
      sb.insert(1, "xx");
      System.out.println("(insert) : " + sb);
      
      // deleteCharAt 删除指定位置的字符
      sb.deleteCharAt(2);
      System.out.println("(deleteCharAt) : " + sb);
      
      // delete 删除指定范围的字符
      sb.delete(2, sb.length());
      System.out.println("(delete) : " + sb);
      
      // replace 替换缓冲区指定范围的字符串
      sb.replace(1, 2, "b");
      System.out.println("(replace) : " + sb);
      
      // reverse 反转缓冲区内的字符串
      sb.reverse();
      System.out.println("(reverse) : " + sb);
      
      // substring 截取字符串,不会操作原缓冲区,返回一个新的字符串
      String str = sb.substring(1, 2);
      System.out.println("(substring) : sb = " + sb + ", str = " + str);
    • 运行结果
      Java 字符串和字符串缓冲区的常用方法_第4张图片

  • StringBufferStringBuilder 具有相同的方法,区别在于 StringBuffer线程安全的,而 StringBuilder线程不安全的

你可能感兴趣的:(Java)