Re: an enum question - how to convert enum type to a string?

Discussion in 'Java' started by Eric Sosman, Feb 26, 2010.

  1. Eric Sosman

    Eric Sosman Guest

    On 2/26/2010 8:33 AM, www wrote:
    > Hi,
    >
    > I am new to enum. Suppose I have the following enum
    >
    > packcage A.B.C
    >
    > enum CarModel
    > {
    > BMW, HONDA, FORD
    > }
    >
    > In my program, I need to get the string "CarModel" from enum CarModel.
    > Is there a way to do it?
    >
    > Note: I am not talking about getting string "BMW", "HONDA" etc. I know
    > CarModel.BMW.toString() will give me "BMW".
    >
    > I also prefer to get "CarModel" string, not "A.B.C.CarModel".


    I'm not sure what you start with: From what, exactly, do you
    want to derive the String? If you have a reference and you think
    it points to an enum CarModel object, you could do something like

    Object obj = ...; // might be FORD
    String name = obj.getClass().getName();
    name = name.substring(name.lastIndexOf('.') + 1);
    if (name.contains("$"))
    name = name.substring(name.lastIndexOf('$') + 1);

    --
    Eric Sosman
    lid
     
    Eric Sosman, Feb 26, 2010
    #1
    1. Advertisements

  2. Eric Sosman

    Mayeul Guest

    www wrote:
    > I will just do this:
    >
    > enum CarModel
    > {
    > BMW, HONDA, FORD;
    >
    > static String getEnumName(){ return "CarModel";}
    >
    > }


    If this is enough to do what you want, then the getEnumName() method is
    unnecessary.

    You may just call CarModel.class.getName()

    I was about to say you could just use the String litteral "CarModel",
    but that would actually be weak to typos, while CarModel.class.getName()
    is not.

    --
    Mayeul
     
    Mayeul, Feb 26, 2010
    #2
    1. Advertisements

  3. Eric Sosman

    Eric Sosman Guest

    On 2/26/2010 9:06 AM, www wrote:
    > Eric Sosman wrote:
    >
    >
    >>
    >> I'm not sure what you start with: From what, exactly, do you
    >> want to derive the String? If you have a reference and you think
    >> it points to an enum CarModel object, you could do something like
    >>
    >> Object obj = ...; // might be FORD
    >> String name = obj.getClass().getName();
    >> name = name.substring(name.lastIndexOf('.') + 1);
    >> if (name.contains("$"))
    >> name = name.substring(name.lastIndexOf('$') + 1);

    >
    > Thank you. It is hard for me to explain why I need what I am asking for.
    >
    > I feel your method is a little inconvenient for the client code. I think
    > I will just do this:
    >
    > enum CarModel
    > {
    > BMW, HONDA, FORD;
    >
    > static String getEnumName(){ return "CarModel";}
    >
    > }


    Okay, but the caller must write `CarModel.getEnumName()'
    to call the method. That is, the caller already *knows* the
    identifier `CarModel', and might have just said `"CarModel"'.
    Oh, well -- this way you get an opportunity to internationalize,
    so `CarModel.getEnumName()' might return "Automodell" in a
    German-speaking locale.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Feb 26, 2010
    #3
  4. Eric Sosman

    Mayeul Guest

    www wrote:
    > I just tested. CarModel.class.getName() gives me "A.B.C.CarModel", not
    > "CarModel". Then I have to extract the string out.


    My mistake, I meant CarModel.class.getSimpleName()
    Which is only available since Java 1.5, I had forgotten. Still, a better
    idea if available.

    --
    Mayeul
     
    Mayeul, Feb 26, 2010
    #4
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Toska

    convert string value to enum

    Toska, Jul 18, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    519
    Toska
    Jul 18, 2004
  2. Toska

    convert string value to enum

    Toska, Jul 18, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    428
    Toska
    Jul 18, 2004
  3. =?Utf-8?B?TWlrZSBNb29yZQ==?=

    Connection String object Convert to String Variable Type

    =?Utf-8?B?TWlrZSBNb29yZQ==?=, Oct 26, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,193
    Norman Yuan
    Oct 26, 2004
  4. guoqi zheng

    convert type string to type guid

    guoqi zheng, Jul 2, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    13,796
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Jul 2, 2005
  5. pluck

    Convert String to Enum

    pluck, Oct 25, 2006, in forum: Java
    Replies:
    1
    Views:
    17,129
    Robert Klemme
    Oct 25, 2006
  6. Replies:
    6
    Views:
    1,117
    Greg Comeau
    Oct 20, 2005
  7. FE
    Replies:
    6
    Views:
    713
    Balog Pal
    Aug 4, 2009
  8. Wojtek
    Replies:
    1
    Views:
    600
Loading...