close


[JAVA]String-取出字串某個位置的字元的方法:charAt、codePointAt、codePointBefore、codePointCount、subSequence、getChars、toCharArray

charcharAt(int index)
返回指定索引處的 char 值。


使用方式:
                說明:取出字串 位置在索引為1的字元。
                String string = "java";
System.out.println(string.charAt(1)); // a



intcodePointAt(int index)
返回指定索引處的字元(Unicode 程式碼點)。
intcodePointBefore(int index)
返回指定索引之前的字元(Unicode 程式碼點)。
intcodePointCount(int beginIndex, int endIndex)
返回此 String 的指定文本範圍中的 Unicode 程式碼點數。


使用方式:
                 說明:取出字串 位置在索引為1的字元的Unicode。
                 System.out.println(string.codePointAt(1));//97
             
                 說明:取出字串 位置在索引為1的前一個字元的Unicode。
System.out.println(string.codePointBefore(1));//106

說明:依字串指定範圍中,算出總數。
System.out.println(string.codePointCount(0,3));//3




CharSequencesubSequence(int beginIndex, int endIndex)
返回一個新的字元序列,它是此序列的一個子序列。


使用方式:
                  說明:切字串從索引第1位到第3位
                  String string = "java";
                  System.out.println(string.subSequence(1, 3));//av



voidgetChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
將字元從此字元串複製到目標字元陣列。


使用方式:






srcBegin - 字元串中要複製的第一個字元的索引。srcEnd - 字元串中要複製的最後一個字元之後的索引。dst - 目標陣列。dstBegin - 目標陣列中的起始偏移量。 char[] chars1 = new char[] {'j','a','v','a'};String string6 = "12356";string6.getChars(0,3,chars1,1);System.out.println(chars1);//j123


char[]toCharArray()
將此字元串轉換為一個新的字元陣列。


使用方式:
                說明:將字串轉為字元陣列
                String string19 = "java";
char [] chars2 = string19.toCharArray();



參考:
String API



 













其它文章

arrow
arrow

    PG Levin Li 發表在 痞客邦 留言(2) 人氣()