问题来源: http://www.cnblogs.com/del/archive/2008/11/04/1209070.html#1360634

代码文件:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

const
  str = '30,80:30:BD6B09:10:1850:苏州:88&50,140:120:976D00:8:150:苏州:88&' +
        '130,80:150:C50023:5:850:苏州:88&100,140:280:CAE5E8:15:450:苏州:88&' +
        '180,190:190:780062:7:3850:苏州:88&130,180:30:BD6B09:10:1850:无锡:88&' +
        '150,40:120:976D00:8:150:无锡:88&90,180:150:C50023:5:850:无锡:88&' +
        '10,80:280:CAE5E8:15:450:无锡:88&150,120:190:780062:7:3850:无锡:88';

type
  TMyRec = record
    Longitude : Single;     {经度}
    Latitude  : Single;     {纬度}
    Angle     : Single;     {角度}
    Color     : TColor;     {颜色}
    Size      : Cardinal;   {大小}
    Value     : Cardinal;   {值}
    Name      : string;     {名称}
    Number    : Integer;    {编号}
  end;

  TMyArr = array of TMyRec;

{转换过程}
procedure MyStr2Arr(const str: string; var Arr: TMyArr);
var
  List1,List2,List3: TStringList;
  i: Integer;
begin
  SetLength(Arr, 0);

  List1 := TStringList.Create;
  List2 := TStringList.Create;
  List3 := TStringList.Create;

  List1.Delimiter := '&';
  List1.DelimitedText := str;

  List2.Delimiter := ':';
  for i := 0 to List1.Count - 1 do
  begin
    List2.DelimitedText := List1[i];
    if List2.Count <> 7 then Exit; {原始数据不合法}

    List3.CommaText := List2[0];

    SetLength(Arr, Length(Arr)+1);
    with Arr[High(Arr)] do
    begin
      Longitude := StrToFloatDef(List3[0], 0);
      Latitude  := StrToFloatDef(List3[1], 0);
      Angle     := StrToFloatDef(List2[1], 0);
      Color     := TColor(StrToIntDef(List2[2], 0));
      Size      := StrToIntDef(List2[3], 0);
      Value     := StrToIntDef(List2[4], 0);
      Name      := List2[5];
      Number    := StrToIntDef(List2[6], 0);
    end;
  end;

  List1.Free;
  List2.Free;
  List3.Free;
end;

{测试}
procedure TForm1.Button1Click(Sender: TObject);
var
  MyDataArr: TMyArr;
  i: Integer;
begin
  MyStr2Arr(str, MyDataArr);

  for i := 0 to Length(MyDataArr) - 1 do
    with MyDataArr[i] do
      Memo1.Lines.Add(Format('%f,%f: %f,%d,%d,%d,%s,%d',
                     [Longitude, Latitude, Angle, Color, Size, Value, Name, Number]));

  Memo1.Lines.Add('');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Memo1.Clear;
  Memo1.Align := alTop;
  Memo1.ScrollBars := ssBoth;
end;

end.

窗体文件:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 227
  ClientWidth = 304
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 120
    Top = 191
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
  object Memo1: TMemo
    Left = 24
    Top = 0
    Width = 185
    Height = 185
    Lines.Strings = (
      'Memo1')
    TabOrder = 1
  end
end

posted on 2008-11-04 13:23  万一  阅读(1637)  评论(0编辑  收藏  举报