Is there a Util to convert US State Name to State Code. eg. Arizona to AZ?

前端 未结 8 572
灰色年华
灰色年华 2020-12-14 16:32

I need to convert full state name to its official state address code. For example from the String New York, I need to produce NY. Now I could put this all in a hashmap, bu

相关标签:
8条回答
  • 2020-12-14 17:11

    As key and value:

    public enum State {
    
        AK("AK", "Alaska"),
        AL("AL", "Alabama"),
        AR("AR", "Arkansas"),
        AZ("AZ", "Arizona"),
        CA("CA", "California"),
        CO("CO", "Colorado"),
        CT("CT", "Connecticut"),
        DE("DE", "Delaware"),
        FL("FL", "Florida"),
        GA("GA", "Georgia"),
        HI("HI", "Hawaii"),
        IA("IA", "Iowa"),
        ID("ID", "Idaho"),
        IL("IL", "Illinois"),
        IN("IN", "Indiana"),
        KS("KS", "Kansas"),
        KY("KY", "Kentucky"),
        LA("LA", "Louisiana"),
        MA("MA", "Massachusetts"),
        MD("MD", "Maryland"),
        ME("ME", "Maine"),
        MI("MI", "Michigan"),
        MN("MN", "Minnesota"),
        MO("MO", "Missouri"),
        MS("MS", "Mississippi"),
        MT("MT", "Montana"),
        NC("NC", "NorthCarolina"),
        ND("ND", "NorthDakota"),
        NE("NE", "Nebraska"),
        NH("NH", "NewHampshire"),
        NJ("NJ", "NewJersey"),
        NM("NM", "NewMexico"),
        NV("NV", "Nevada"),
        NY("NY", "NewYork"),
        OH("OH", "Ohio"),
        OK("OK", "Oklahoma"),
        OR("OR", "Oregon"),
        PA("PA", "Pennsylvania"),
        RI("RI", "RhodeIsland"),
        SC("SC", "SouthCarolina"),
        SD("SD", "SouthDakota"),
        TN("TN", "Tennessee"),
        TX("TX", "Texas"),
        UT("UT", "Utah"),
        VA("VA", "Virginia"),
        VT("VT", "Vermont"),
        WA("WA", "Washington"),
        WI("WI", "Wisconsin"),
        WV("WV", "WestVirginia"),
        WY("WY", "Wyoming");
    
        private final String key;
        private final String value;
    
        State(String key, String value) {
            this.key = key;
            this.value = value;
        }
    }
    
    0 讨论(0)
  • 2020-12-14 17:13

    I think the simplest way would be with a HashMap. Even if there was a library to convert it, it would probably use the same thing.

    Map<String, String> states = new HashMap<String, String>();
    states.put("Arizona", "AZ");
    states.put("California", "CA");
    // So on and so forth...
    
    // Then you could create a method like
    public String toStateCode(String s) {
        return states.get(s);
    }
    
    0 讨论(0)
提交回复
热议问题