JavaでUSC2文字列を切り出す方法

Javaコード:

public static String cutString(String s, int length) throws Exception{
byte[] bytes = s.getBytes(“Unicode");
int n = 0;
int i = 2;//[0]=-2 [1]=-1
for (; i < bytes.length && n < length; i++) {
if (i % 2 == 1) {
n++;
} else {
if (bytes[i] != 0) {
n++;
}
}
}
if (i % 2 == 1) {
if (bytes[i – 1] != 0)
i = i – 1;
else
i = i + 1;
}
return new String(bytes, 0, i, “Unicode");
}

Java

Posted by arkgame