Delphi : 分割字符串string,取出截取中间字符串

Delphi : 分割字符串string,取出截取中间字符串

  (2012-05-17 09:50:26)
标签: 

it

分类: 软件_Software

Function FindStr(BegSprStr,EdSpStr:String;var Source:String):String;
var
BegStr1Len,EdSpStr2Len,BegStr1Index,EdSpStr2index:integer;
Temp:String;
begin
Temp:=Source;
BegStr1Index:=Pos(BegSprStr,Temp);
BegStr1Len:=Length(BegSprStr);
delete(Temp,BegStr1Index,BegStr1Index+BegStr1Len-1);
EdSpStr2index:=Pos(EdSpStr,Temp);
EdSpStr2Len:=Length(EdSpStr);
Result:=Copy(Temp,1,EdSpStr2index-1);
Delete(Temp,1,EdSpStr2index+EdSpStr2Len-1);
Source:=temp;
end


該函數的功能就是取兩個相隔符之間的字符.
var Source:string
Source:=<1111>string1<1122><2233>string2<3344><4455>string3<5566>;
string1:=FindStr('<1111>','<1122>',Source);
String2:=FindStr('<2233>','<3344>',Source);
String3:=FindStr('<4455>','<5566>',Source);


//---------------------------------------------我想取得一个email地址的用户名

function myuser(email:string):string;
var
i,n:integer;
begin
n:=pos('@',email);
result:=copy(email,0,n-1);//改了一下
end;
//---------------------------------------------
下面就可以取出
leftstr(emaddress,Pos('@',emaddress))
//---------------------------------------------
uses IdGlobal;
User := Fetch(EmailAddress, '@');
//--------------------------------
ExtractStrings(要分隔的字符串,分隔字符串用的字符集合,分隔后排在第一位的消去的字符集合,字符串列表对象)


另一个函数SplitString

Function SplitString(const source,ch:string):TStringList;
var
temp, t2 : string;
i : integer;
begin
result := TStringList.Create;
temp := source;
i := pos(ch,source);
while i<>0 do begin
t2 := copy(temp,0,i-1);
if (t2<>'') then result.Add(t2);
delete(temp,1,i-1+Length(ch));
i:=pos(ch,temp);
end;
result.Add(temp);
end;
例子:

st := SplitString('[email protected]', '@');

st[0] = 'xxx';
st[1] = 'hoho.com';
-----------------------------------------------------------------------------

因为喜欢用文本来记录数据,比如帐号密码等等,一个游戏帐户一行

帐号1,密码1,游戏区11

帐号2,密码2,游戏区11

需要用到分割字符串函数。

delphi的Classes有ExtractStrings函数,感觉用起来不好,后来网上找了个,如下

function SplitString(pString:Pchar;psubString:PChar):TStringList;
var
   nSize,SubStringSize:DWord;
   intI,intJ,intK:DWORD;
   ts:TStringList;
   curChar:Char;
   strString:string;
   strsearchSubStr:string;
begin
   nSize:=strLen(pString);
   SubStringSize:=strLen(PSubString);
   ts:=TStringList.Create;
   strstring:='';
   inti:=0;
   while intI<=(nSize-1) do
   begin
      if (nsize-inti)>= substringSize then
      begin
          if ((PString+intI)^=pSubString^) then
          begin
             intk:=inti;
            strSearchSubStr:='';
            curchar:=(pstring+intk)^;
            strsearchSubStr:=strSearchSubStr+Curchar;
            intk:=intk+1;
            for intj:= 1 to SubStringSize-1  do
            begin
               if ((pString+intk)^=(PSubString+intj)^) then
               begin
                  curchar:=(pstring+intk)^;
                  intk:=intk+1;
                  strsearchSubStr:=strSearchSubStr+Curchar;
            end
            else begin
              inti:=intk;
      

你可能感兴趣的:(delphi)