如何获取与 Java 中的 Charset 对象关联的数字代码页标识符(即 1252)?我可以调用 displayName() 方法,但它返回字母数字标识符(如“windows-1252”、“cp-1252”、“CP1252”、...),而不仅仅是 int 代码。
在 .NET 中 Encoding 类中存在一个整数 CodePage 属性,但我在 Java 中找不到等效的方法。
谢谢。
如何获取与 Java 中的 Charset 对象关联的数字代码页标识符(即 1252)?我可以调用 displayName() 方法,但它返回字母数字标识符(如“windows-1252”、“cp-1252”、“CP1252”、...),而不仅仅是 int 代码。
在 .NET 中 Encoding 类中存在一个整数 CodePage 属性,但我在 Java 中找不到等效的方法。
谢谢。
从您提供的示例中,您可以使用正则表达式:
private static final Pattern NUMERIC_CODEPAGE_PATTERN = Pattern.compile("[^\\d]*(\\d+)");
...
String displayName = charSet.displayName();
Matcher matcher = NUMERIC_CODEPAGE_PATTERN.matcher(displayName);
if(matcher.matches())
{
String numericCodeString = matcher.group(1);
int numericCode = Integer.parseInt(numericCodeString);
}