上一章節傳送門 --> jdk8源碼分析筆記1 --> Object.classjava
挑選了String.class大部分代碼,進行@Test測試。app
話很少說,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...測試