我以前写的一个函数: { 返回值:切割的字符串个数 切割的字符串放在lstData中 sSplit:分隔符 sSource:待切割的字符串 } function MySplitString(const sSource, sSplit: String;var lstData: TStrings): Integer; var iIndex, iPreIndex: Integer; pStr: PChar; begin lstData.Clear; pStr := PChar(sSource); iPreIndex := 0; iIndex := Pos(sSplit, pStr); //Èç¹ûÒ»¸öÒ²ÕÒ²»µ½£¬Ôò½« sSource Ö±½Ó¿½±´¸øÄ¿±ê if iIndex = 0 then lstData.Add(sSource); while (iIndex > iPreIndex) and (iPreIndex <= Length(sSource))do begin lstData.Add(Copy(pStr + iPreIndex, 1, iIndex - iPreIndex - 1)); iPreIndex := iIndex + Length(sSplit) - 1; iIndex := Pos(sSplit, pStr + iPreIndex) + iPreIndex; if iIndex = iPreIndex then iIndex := Length(sSource) + 1; end; result := lstData.Count; end;
var Str :String; Qty :integer;Qty:=Copy(Str,Pos(';',Str)+1,Pos('/',Str)-Pos(';',Str)-1);
分割符个数: function GetSplitCnt(Source,Split:string):integer; var idx:integer; begin result := 0; while true then begin idx := pos(Split,Source); if idx < 0 then break; inc(result); Source := copy(Source,idx+1,length(Source)); end; end;
不好意思,把while true then 改成while true do
下面的函数返回一个数组,数组的每个元素是一个分隔符的位置,数组的长度就是分隔符的数量typedef TDividers = array of integer; function FindDividers(const Str: string; Divider: char): TDividers; var s: string; n, p: integer; begin SetLength(Result, 0); n := 0; s := Str; while true do begin p := Pos(Divider, s); s := Copy(s, p+1, MaxInt); SetLength(Result, n+1); Result[n] := p; inc(n); end end;
to lovedandan(工作了~郁闷了~) :你的 while true do 是死循环
某例子而已 function StrCpm(Str:String;s_chr:char): Integer; var i : Integer; temp : Integer begin result:= 0 ; temp:=0; for i:= 1 to Lenght(Str) do begin if Str[i]= s_chr then temp:= temp+1 ; end; result:= temp; end;
{
返回值:切割的字符串个数
切割的字符串放在lstData中
sSplit:分隔符
sSource:待切割的字符串
}
function MySplitString(const sSource, sSplit: String;var lstData: TStrings): Integer;
var
iIndex, iPreIndex: Integer;
pStr: PChar;
begin
lstData.Clear;
pStr := PChar(sSource);
iPreIndex := 0;
iIndex := Pos(sSplit, pStr);
//Èç¹ûÒ»¸öÒ²ÕÒ²»µ½£¬Ôò½« sSource Ö±½Ó¿½±´¸øÄ¿±ê
if iIndex = 0 then
lstData.Add(sSource); while (iIndex > iPreIndex) and (iPreIndex <= Length(sSource))do
begin
lstData.Add(Copy(pStr + iPreIndex, 1, iIndex - iPreIndex - 1));
iPreIndex := iIndex + Length(sSplit) - 1;
iIndex := Pos(sSplit, pStr + iPreIndex) + iPreIndex;
if iIndex = iPreIndex then
iIndex := Length(sSource) + 1;
end;
result := lstData.Count;
end;
Qty :integer;Qty:=Copy(Str,Pos(';',Str)+1,Pos('/',Str)-Pos(';',Str)-1);
function GetSplitCnt(Source,Split:string):integer;
var
idx:integer;
begin
result := 0;
while true then
begin
idx := pos(Split,Source);
if idx < 0 then break;
inc(result);
Source := copy(Source,idx+1,length(Source));
end;
end;
function FindDividers(const Str: string; Divider: char): TDividers;
var s: string; n, p: integer;
begin
SetLength(Result, 0);
n := 0;
s := Str;
while true do
begin
p := Pos(Divider, s);
s := Copy(s, p+1, MaxInt);
SetLength(Result, n+1);
Result[n] := p;
inc(n);
end
end;
function StrCpm(Str:String;s_chr:char): Integer;
var
i : Integer;
temp : Integer
begin
result:= 0 ;
temp:=0;
for i:= 1 to Lenght(Str) do
begin
if Str[i]= s_chr then
temp:= temp+1 ;
end;
result:= temp;
end;