Delphi - Is there a better way to get state abbreviations from state names

Posted by Bill on Stack Overflow See other posts from Stack Overflow or by Bill
Published on 2010-04-28T22:12:11Z Indexed on 2010/04/28 22:17 UTC
Read the original article Hit count: 465

Filed under:
|
|

const
states : array [0..49,0..1] of string =
(
('Alabama','AL'),
('Montana','MT'),
('Alaska','AK'),
('Nebraska','NE'),
('Arizona','AZ'),
('Nevada','NV'),
('Arkansas','AR'),
('New Hampshire','NH'),
('California','CA'),
('New Jersey','NJ'),
('Colorado','CO'),
('New Mexico','NM'),
('Connecticut','CT'),
('New York','NY'),
('Delaware','DE'),
('North Carolina','NC'),
('Florida','FL'),
('North Dakota','ND'),
('Georgia','GA'),
('Ohio','OH'),
('Hawaii','HI'),
('Oklahoma','OK'),
('Idaho','ID'),
('Oregon','OR'),
('Illinois','IL'),
('Pennsylvania','PA'),
('Indiana','IN'),
('Rhode Island','RI'),
('Iowa','IA'),
('South Carolin','SC'),
('Kansas','KS'),
('South Dakota','SD'),
('Kentucky','KY'),
('Tennessee','TN'),
('Louisiana','LA'),
('Texas','TX'),
('Maine','ME'),
('Utah','UT'),
('Maryland','MD'),
('Vermont','VT'),
('Massachusetts','MA'),
('Virginia','VA'),
('Michigan','MI'),
('Washington','WA'),
('Minnesota','MN'),
('West Virginia','WV'),
('Mississippi','MS'),
('Wisconsin','WI'),
('Missouri','MO'),
('Wyoming','WY')
);
function getabb(state:string):string;
var
  I:integer;
begin
  for I := 0 to length(states) -1 do
  if lowercase(state) = lowercase(states[I,0]) then
  begin
    result:= states[I,1];
  end;
end;
function getstate(state:string):string;
var
  I:integer;
begin
  for I := 0 to length(states) -1 do
  if lowercase(state) = lowercase(states[I,1]) then
  begin
    result:= states[I,0];
  end;
end;
procedure TForm2.Button1Click(Sender: TObject);
begin
 edit1.Text:=getabb(edit1.Text);
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
 edit1.Text:=getstate(edit1.Text);
end;

end.

Is there a bette way to do this?

© Stack Overflow or respective owner

Related posts about delphi

Related posts about delphi-2009