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. Advertising

  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. Advertising

  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. Advertising

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. guoqi zheng

    convert type string to type guid

    guoqi zheng, Jul 2, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    13,385
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Jul 2, 2005
  2. Replies:
    6
    Views:
    960
    Greg Comeau
    Oct 20, 2005
  3. Wojtek
    Replies:
    1
    Views:
    505
  4. Lew
    Replies:
    0
    Views:
    541
  5. Roedy Green
    Replies:
    0
    Views:
    737
    Roedy Green
    Feb 27, 2010
Loading...

Share This Page