求分析字符串的高效率函数过程 ( 积分: 100 )

  • 主题发起人 主题发起人 初学DELPHI者
  • 开始时间 开始时间

初学DELPHI者

Unregistered / Unconfirmed
GUEST, unregistred user!
如何将如下的字符串根据空格划分数组。。。
123 234 adgg 233 xddgdg
 
如何将如下的字符串根据空格划分数组。。。
123 234 adgg 233 xddgdg
 
自己写个函数扫描顺序一下,很简单的:
procedure TForm1.FormCreate(Sender: TObject);
var
slOut: TStrings;

procedure SplitString(sInput: String; slOut: TStrings);
var
idx, iOldIdx, iLen: Integer;
begin
idx := 1;
iLen := Length(sInput);
while idx <= iLen do
begin
iOldIdx := idx;
while (idx <= iLen) and (sInput[idx] <> ' ') do
Inc(idx);
if iOldIdx <> idx then
slOut.Append(Copy(sInput, iOldIdx, idx - iOldIdx));
while (idx <= iLen) and (sInput[idx] = ' ') do
Inc(idx);
end;
end;

begin
slOut := TStringList.Create;
try
SplitString('123 234 adgg 233 xddgdg', slOut);
Caption := slOut.Text;
finally
slOut.Free;
end;
end;
 
ExtractStrings

var
i:integer;
instring:string;
list:TStringList;
begin
instring:='你的字符串';
list := TStringList.Create;
ExtractStrings([' '], [], PChar(instring), list); // 分割
end;
 
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Lines.Delimiter := ' ';
Memo1.Lines.DelimitedText := '123 234 adgg 233 xddgdg';
ShowMessage(Memo1.Lines[0]);
end;

end.
 
指针(PChar)移动 Inc(p);
 
摘至jclstrings.pas
procedure StrToStrings(S, Sep: AnsiString; const List: TStrings; const AllowEmptyString: Boolean = True);
var
I, L: Integer;
Left: AnsiString;
begin
Assert(List <> nil);
List.BeginUpdate;
try
List.Clear;
L := Length(Sep);
I := Pos(Sep, S);
while I > 0 do
begin
Left := StrLeft(S, I - 1);
if (Left <> '') or AllowEmptyString then
List.Add(Left);
System.Delete(S, 1, I + L - 1);
I := Pos(Sep, S);
end;
if S <> '' then
List.Add(S); // Ignore empty strings at the end.
finally
List.EndUpdate;
end;
end;

procedure StrIToStrings(S, Sep: AnsiString; const List: TStrings; const AllowEmptyString: Boolean = True);
var
I, L: Integer;
LowerCaseStr: string;
Left: AnsiString;
begin
Assert(List <> nil);
LowerCaseStr := StrLower(S);
Sep := StrLower(Sep);
L := Length(Sep);
I := Pos(Sep, LowerCaseStr);
List.BeginUpdate;
try
List.Clear;
while I > 0 do
begin
Left := StrLeft(S, I - 1);
if (Left <> '') or AllowEmptyString then
List.Add(Left);
System.Delete(S, 1, I + L - 1);
System.Delete(LowerCaseStr, 1, I + L - 1);
I := Pos(Sep, LowerCaseStr);
end;
if S <> '' then
List.Add(S); // Ignore empty strings at the end.
finally
List.EndUpdate;
end;
end;
 
var ii ,tempCount: integer;
tempStr : String;
strArr : array of String;

ii := 0;
tempStr := '123 234 adgg 233 xddgdg ';
repeat
tempCount := Pos(' ',tempStr);
if tempCount > 0 then
begin
SetLength(strArr,ii+1);
strArr[ii] := copy(tempStr,1,tempCount-1);
tempStr := trim(copy(tempStr,tempCount,length(tempStr)));
ii := ii + 1;
end
else
begin
SetLength(strArr,ii+1);
strArr[ii] := tempStr;
end;
until tempCount = 0;

arrStr 即是你想要的数组
 
谢谢大家的指点!
大家是否可以再评论一下,哪个程序的处理效率更高一点呢??[:)]
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=2246444
 
多人接受答案了。
 
后退
顶部