getBytes(“Windows-31J”)でShift-JIS文字列の長さを取得する
サンプルコード
package com.arkgame.study;
import java.io.UnsupportedEncodingException;
public class ByteLengthDemo {
public static void main(String[] args) {
String str = "あ";
int len = 0;
try {
len = str.getBytes("Windows-31J").length;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("長さ:" + len);
}
}
長さ:2