简体   繁体   English

“ - ”在枚举java中分隔字符串

[英]“-” separated strings in enum java

I have a problem in creating an enumeration in Java using "-"-separated strings: 我在使用“ - ”创建Java中的枚举时遇到问题 - 分隔的字符串:

public enum CipherList{

   RSA-MD5,AES128-SHA,AES256-SHA;
}

I am getting compilation errors. 我收到编译错误。

The - symbol may not be used in an identifier in Java. -符号不能在Java中的标识符中使用。 (How would RSA-MD5 be parsed if RSA and MD5 happened to be integers?) (如果RSAMD5恰好是整数, RSA-MD5将如何解析?)

I suggest you use 我建议你用

RSA_MD5, AES128_SHA, AES256_SHA;

according to the Java coding conventions for constants related question . 根据常规相关问题的Java编码约定。

Enum constants have to use valid Java identifiers, and identifiers are not allowed to contain dashes. 枚举常量必须使用有效的Java标识符,并且不允许标识符包含破折号。

You could, for example, replace dashes with underscores: 例如,您可以用下划线替换短划线:

public enum CipherList{
   RSA_MD5, AES128_SHA, AES256_SHA;
}

If you want to use exact strings using enums you can use following approach. 如果要使用枚举使用完全字符串,可以使用以下方法。

enum CipherList{
   CHIP_ONE("RSA-MD5"),CHIP_TWO("AES128-SHA"),CHIP_THREE("AES256-SHA");
   private String code;
   CipherList(String code) {
      code= code;
   }
   String getcode() {
      return code;
   } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM