2014-01-28 51 views

回答

7

這裏沒有,做它在一個呼叫的方法,但offsetByCodePoints()將幫助你做到這一點。

static String substring(String str, int idx, int len) { 
    return str.substring(idx, str.offsetByCodePoints(idx, len)); 
} 
+1

是不是必須是'substring(str.offsetByCodePoints(0,idx),str.offsetByCodePoints(0,idx + len))'? – caw

+0

這取決於'idx'參數是字符偏移量還是碼點偏移量。字符串方法通常採用字符偏移量,而那些使用代碼點的方法旨在將代碼點偏移量轉換爲字符偏移量。 – erickson

相關問題