![](/img/trans.png)
[英]Is there a Java equivalent of the Javascript method fromCharCode()?
[英]What is the java equivalent to javascript's String.fromCharCode?
什么是Java相當於javascript的:
String.fromCharCode(n1, n2, ..., nX)
那將是如下:
public static String fromCharCode(int... codePoints) {
StringBuilder builder = new StringBuilder(codePoints.length);
for (int codePoint : codePoints) {
builder.append(Character.toChars(codePoint));
}
return builder.toString();
}
請注意,無法保證轉換為char
,因為codepoint值可能超過char
的上限(65535)。 當Unicode 3.1還沒有出現超過65535個字符時, char
就建立在黑暗的Java時代。
更新 : String
實際上是一個帶有int[]
的構造函數 (從Java 1.5開始引入,從頭頂不知道它),它正確地處理了這個問題。 以上內容可簡化如下:
public static String fromCharCode(int... codePoints) {
return new String(codePoints, 0, codePoints.length);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.