如何在资本化中获得枚举价值

发布于 2025-02-10 12:09:50 字数 289 浏览 2 评论 0 原文

我们创建枚举类,并以完整的大写编写所有枚举成员。

public enum Color {
    RED,
    YELLOW,
    BLACK
}

我们如何在Java中获得资本化的价值(在大写速度中写下单词的第一个字母,以及小写字母的其余字母)。

意思是,当我获取 color 枚举的值时,我应该以 red yellow> yellow black 来获取

We create Enum classes and write all enum members in Full Uppercase.

public enum Color {
    RED,
    YELLOW,
    BLACK
}

How can we get the values in Capitalization (Writing the first letter of a word in uppercase, and the rest of the letters in lowercase) in java.

Meaning, when I fetch the Value of Color Enum, I should get as Red, Yellow, Black

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

執念 2025-02-17 12:09:50

如果您想使用 ()方法,您别无选择,只能在代码中写入资本化的价值。

如果要获得每种颜色的“显示名称»”,则可以在枚举中添加一个字符串字段,如下示例:

public enum Color {
    RED("Red"),
    YELLOW("Yellow"),
    BLACK("Black");

    String displayName;

    Color(String displayName){
         this.displayName = displayName;
    }
}

然后,您可以使用 red.displayname 访问大写的名称。 。

最后一个解决方案,您可以手动资本化名称,并使用以下代码:

String str = RED.name();
String displayName = str.substring(0, 1).toUpperCase() + str.substring(1).toLowerCase();

If you want to use java enums name() method, you have no choice but to write the values in Capitalization in your code.

If you want to get a «display name» for each color, you can add a String field to your enum, like in the example below:

public enum Color {
    RED("Red"),
    YELLOW("Yellow"),
    BLACK("Black");

    String displayName;

    Color(String displayName){
         this.displayName = displayName;
    }
}

Then you can access the capitalized name with RED.displayName for example.

Last solution, you can manually capitalize the name, with the code below:

String str = RED.name();
String displayName = str.substring(0, 1).toUpperCase() + str.substring(1).toLowerCase();
女中豪杰 2025-02-17 12:09:50

枚举不仅可以是简单常数,就像您在摘要中所拥有的那样。查看 https://docs.oracle.com/javase.com/javase.com/javase/tutorial/ java/javaoo/enum.html ,尤其是行星示例。

因此,当然,您可以覆盖ToString()方法,以及您需要的其他任何内容才能运行表示。

An enum can be more than just the simple constant as you have it in your snippet. Check out https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html, especially the planet example.

So of course you can override the toString() method and whatever else you need to get your representation running.

乱了心跳 2025-02-17 12:09:50

如果您能够使用Apache stringutils,则可以首先使用 string.tolowercase() ,然后 string> strignutils.capitalize() ,例如“红色”转换为“红色” “红色”至“红色”。

这是您的原始枚举声明:

public enum Color {
    RED,
    YELLOW,
    BLACK
}

这是一个示例:

  • 迭代每个枚举值
  • 通过调用 name()
  • 调用 tolowercase(tolowercase(),并打印该值
  • 调用 Capitalize()带有小写的值,并打印出
  • 一个最终的 println(),因此在这里不全部堆叠输出,
for (Color value : Color.values()) {
    String name = value.name();
    System.out.println("       name: " + name);
    
    String lowerCased = name.toLowerCase();
    System.out.println(" lowerCased: " + lowerCased);
    
    String capitalized = StringUtils.capitalize(lowerCased);
    System.out.println("capitalized: " + capitalized);
    
    System.out.println();
}

这是该代码的输出:

       name: RED
 lowerCased: red
capitalized: Red

       name: YELLOW
 lowerCased: yellow
capitalized: Yellow

       name: BLACK
 lowerCased: black
capitalized: Black

这显示了如何显示进行转换,留下枚举定义是(大写;“红色”等)。

如果您想将其整合到枚举定义本身中,则可以做类似的事情:

public enum Color {
    RED,
    YELLOW,
    BLACK;

    public final String getDisplayName() {
        return StringUtils.capitalize(this.name().toLowerCase());
    }
}

并使用这样的事情:

for (Color value : Color.values()) {
    System.out.println(value.getDisplayName());
}

它会产生这样的输出:

Red
Yellow
Black

If you're able to use Apache StringUtils, you could first use String.toLowerCase(), and then StringUtils.capitalize(), converting for example "RED" to "red" to "Red".

Here's your original enum declaration:

public enum Color {
    RED,
    YELLOW,
    BLACK
}

And here's an example that:

  • iterates each enum value
  • prints the name by calling name()
  • calls toLowerCase(), and prints that value
  • calls capitalize() with the lowercase value, and prints that, too
  • one final println() so the output isn't all bunched up
for (Color value : Color.values()) {
    String name = value.name();
    System.out.println("       name: " + name);
    
    String lowerCased = name.toLowerCase();
    System.out.println(" lowerCased: " + lowerCased);
    
    String capitalized = StringUtils.capitalize(lowerCased);
    System.out.println("capitalized: " + capitalized);
    
    System.out.println();
}

Here is the output from that code:

       name: RED
 lowerCased: red
capitalized: Red

       name: YELLOW
 lowerCased: yellow
capitalized: Yellow

       name: BLACK
 lowerCased: black
capitalized: Black

This shows how to do the conversion, leaving the enum definitions are they are (uppercase; "RED", etc).

If you'd like to incorporate this into the enum definition itself, you could do something like this:

public enum Color {
    RED,
    YELLOW,
    BLACK;

    public final String getDisplayName() {
        return StringUtils.capitalize(this.name().toLowerCase());
    }
}

And use it like this:

for (Color value : Color.values()) {
    System.out.println(value.getDisplayName());
}

Which would produce output like this:

Red
Yellow
Black
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文