上一章节传送门 --> jdk8源码分析笔记1 --> Object.class

挑选了String.class大部分代码,进行@Test测试。

话不多说,code演示

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.IntStream;
import org.junit.Test;

public class TestString {

  @Test
  public void testConstructor() {
    String strA = new String();
    System.out.println(strA);//
  }

  @Test
  public void testConstructorByCharArr() {
    char[] charArr = {'a', 'b', 'c'};
    String strA = new String(charArr);
    System.out.println("strA =" + strA);//strA =abc
  }

  @Test
  public void testConstructorByIndex() {
    char[] charArr = {'a', 'b', 'c', 'd', 'e'};
    String strA = new String(charArr, 1, 2);
    System.out.println("strA = " + strA);//strA = bc
  }

  /**
   * 从insArr里面,第offset位置开始拿,拿取的长度是3个,并转换成char,组合成字符串String
   */
  @Test
  public void testConstructorByIntArr() {
    int[] insArr = {65, 66, 67, 68, 69, 70, 71};//A,B,C,D,E,F,G
    String strA = new String(insArr, 2, 3);
    System.out.println(strA);//CDE
  }

  /**
   * 从 bytesArr 里面,第offset位置开始拿,拿取的长度是3个,并转换成char,组合成字符串String Charset charset
   */
  @Test
  public void testConstructorByBytes() {
    byte[] bytesArr = {65, 66, 67, 68, 69, 70, 71};
    String strA = new String(bytesArr, 0, 3, StandardCharsets.UTF_8);
    System.out.println(strA);//ABC
    String strB = new String(bytesArr, StandardCharsets.UTF_8);
    System.out.println(strB);//ABCDEFG
    String strC = new String(bytesArr, 0, 3);//Charset.defaultCharset().name();读取系统的编码环境
    System.out.println(strC);//ABCD
  }

  @Test
  public void testConstructorByStringBuff() {
    StringBuffer sbuf = new StringBuffer();
    sbuf.append(1).append('a').append("AA");
    String strA = new String(sbuf);
    System.out.println(strA);//1aAA
  }

  @Test
  public void testConstructorByStringBuild() {
    StringBuilder sbud = new StringBuilder();
    sbud.append(1).append('a').append("AA");
    String strA = new String(sbud);
    System.out.println(strA);//1aAA
  }

  @Test
  public void testStringLength() {
    String strA = "";
    String strB = " ";
    String strC = null;
    System.out.println(strA.length());//0
    System.out.println(strB.length());//3
    System.out.println(strC.length());//throw NullPointerException
  }


  @Test
  public void testStringIsEmpty() {
    String strA = "";
    String strB = null;
    String strC = " ";
    System.out.println(strA.isEmpty());//true
    System.out.println(strC.isEmpty());//false
    System.out.println(strB.isEmpty());//throw NullPointerException
  }

  @Test
  public void testCharAt() {
    String strA = "我爱我的祖国";
    IntStream.range(0, strA.length()).forEach(ints -> System.out.print(strA.charAt(ints)));//我爱我的祖国
  }

  /**
   * 获取字符串下标 index 的 codePoint
   */
  @Test
  public void testCodePointAt() {
    String strA = "ABCDEFG";
    int codePoint = strA.codePointAt(0);
    System.out.println(codePoint);//65
  }

  /**
   * 返回index下标的前一位字符的 codePoint
   */
  @Test
  public void testCodePointBefore() {
    String strA = "ABCDEFG";
    int codePoint = strA
        .codePointBefore(strA.length());//index>0 or throw StringIndexOutOfBoundsException
    System.out.println(codePoint);//65
  }

  /**
   * 增补字符,即代码点为 U+10000~U+10FFFF 的字符,这是 Unicode 5.0 中新增的代码点字符。 在 Java 中一个 Unicode 字符是使用 UTF-16 编码的 char 进行表示的,
   * 也就是一个 char 只能表示 U+0000~U+FFFF 的 Unicode 基本字符(BMP)。 因此在 Java 中需要表示 U+10000~U+10FFFF 的字符需要使用 一对代理字符进行表示,
   * 高代理字符的范围为 U+D800~U+DBFF,低代理字符的范围为 U+DC00~U+DFFF。 比如表示 U+10400 的字符需要两个 char(U+D801, U+DC00)才能表示,
   * 这时的代码点长度为 1,而代码单元长度为 2 转自 https://blog.csdn.net/weixin_40841731/article/details/83617660 没弄明白
   * codePointCount的使用场景
   */
  @Test
  public void testCodePointCount() {
    char[] chs = Character.toChars(0x10400);
    System.out.println(chs.length);
    System.out.printf("U+10400 高代理字符: %04x%n", (int) chs[0]);
    System.out.printf("U+10400 低代理字符: %04x%n", (int) chs[1]);
    String str = new String(chs);
    System.out.println("代码单元长度: " + str.length());
    System.out.println("代码点数量: " + str.codePointCount(0, str.length()));
  }

  @Test
  public void testOffsetByCodePoints() {
    char[] chs = Character.toChars(0x10400);
    String strA = new String(chs);
    System.out.println("\"" + strA + "\".length()==" + strA.length());//"𐐀".length()==2
    int iA = strA.offsetByCodePoints(0, 1);
    System.out.println(iA);//2
    System.out.println("-------分割线-------");
    String strB = "ab";
    System.out.println("\"" + strB + "\".length()==" + strB.length());//"ab".length()==2
    int iB = strB.offsetByCodePoints(0, 1);
    System.out.println(iB);//1

  }

  /**
   * charsArr的长度是不可变的 允许范围内截取的strA长度+dstBegin,不能超过 charsArr的长度,否则抛异常
   */
  @Test
  public void testGetChars() {
    String strA = "我爱我的祖国";
    char[] charsArr = new char[5];
    strA.getChars(0, 5, charsArr, 0);
    System.out.println(String.valueOf(charsArr));
  }

  @Test
  public void testGetBytes() {
    String strA = "ABCDEF";
    byte[] bytes1 = strA.getBytes();
    for (byte b : bytes1) {
      System.out.println(b);
    }
    System.out.println("---------分割线---------");
    byte[] bytes2 = strA.getBytes(StandardCharsets.UTF_8);
    for (byte b : bytes2) {
      System.out.println(b);
    }
  }


  @Test
  public void testCopyOfStr() {
    char[] charsArr = {'A', 'B', 'C'};
    String str = new String(charsArr);
    String strCopy = String.copyValueOf(charsArr);
    System.out.println(str == strCopy);//false 深拷贝
    System.out.println(str.equals(strCopy));//true
  }

  @Test
  public void testEndWithAndStartWith() {
    String strA = "ahbcahg";
    System.out.println(strA.endsWith("hg"));//true
    System.out.println(strA.startsWith("ah"));//true
  }
  @Test
  public void testIndexOf() {
    String strA = "ahbcahg";
    System.out.println(strA.indexOf("a"));//0
    System.out.println(strA.lastIndexOf("ah"));//4
  }

  @Test
  public void testJoinAndSplit() {
    ArrayList<String> strList = new ArrayList<>();
    IntStream.range(0, 3).forEach(ints -> strList.add(ints + "str"));
    String joinStr = String.join(",", strList);
    System.out.println(joinStr);//0str,1str,2str
    System.out.println(joinStr.replace("str", ""));//0,1,2
    System.out.println(joinStr.replaceAll("str", ""));//0,1,2
    Arrays.asList(joinStr.split(",")).forEach(System.out::print);//0str1str2str
  }

  @Test
  public void testFormat() {
    String format = String.format("%d%s喜欢%s没有%s", 1, "我", "代码", "BUG");
    System.out.println(format);//1我喜欢代码没有BUG
  }

  @Test
  public void testValueOf() {
    String strA = "";
    String strB = " ";
    String strC = null;
    System.out.println(strA);//
    System.out.println(strB);//
    System.out.println(strC);//源码做了非空处理
  }

  @Test
  public void testIntern() {
    String str1 = "aaa";
    String str2 = "bbb";
    String str3 = "aaabbb";
    String str4 = str1 + str2;
    String str5 = "aaa" + "bbb";
    System.out.println(str3 == str4); // false
    System.out.println(str3 == str4.intern()); // true
    System.out.println(str3 == str5);// true
  }

}

已经是最新章节了,will be continue...

03-07 16:25