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

前端 未结 8 569
灰色年华
灰色年华 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: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 states = new HashMap();
    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);
    }
    

提交回复
热议问题