比如如下字符串string='<刘德华> <张学友> <成吉思汗> <牛顿> <可乐> <斯琴格尔日> <人> <周杰伦> <张会妹> <温造轮>'我要直接截取中间的汉字,把<>符号都去掉,我查过一些函数,都只能定义一个分隔符,有没有可以同时定义前后分隔符的函数,这样就可以只用一个循环就取出来???有没有别的方法??

解决方案 »

  1.   

    试试这个:
    function StrLeft(const mStr: string; mDelimiter: string): string;
    begin
      Result := Copy(mStr, 1, Pos(mDelimiter, mStr) - 1);
    end; { StrLeft }function ListCount(mList: string; mDelimiter: string = ','): Integer;
    var
      I, L: Integer;
    begin
      Result := 0;
      if mList = '' then Exit;
      L := Length(mList);
      I := Pos(mDelimiter, mList);
      while I > 0 do begin
        mList := Copy(mList, I + Length(mDelimiter), L);
        I := Pos(mDelimiter, mList);
        Inc(Result);
      end;
      Inc(Result);
    end; { ListCount }function ListValue(mList: string; mIndex: Integer; mDelimiter: string = ','): string;
    var
      I, L, K: Integer;
    begin
      L := Length(mList);
      I := Pos(mDelimiter, mList);
      K := 0;
      Result := '';
      while (I > 0) and (K <> mIndex) do begin
        mList := Copy(mList, I + Length(mDelimiter), L);
        I := Pos(mDelimiter, mList);
        Inc(K);
      end;
      if K = mIndex then Result := StrLeft(mList + mDelimiter, mDelimiter);
    end; { ListValue }procedure TForm1.Button1Click(Sender: TObject);
    var
      I: Integer;
      S: string;
    begin
      S := '<刘德华> <张学友> <成吉思汗> <牛顿> <可乐> <斯琴格尔日> <人> <周杰伦> <张会妹> <温造轮>';
      for I := 0 to ListCount(S, '> <') - 1 do
        ShowMessage(ListValue(S, I, '> <'));
    end;
      

  2.   

    procedure TForm1.Button2Click(Sender: TObject);
    const
      Delimiter :String = '> <';
    var
      Str :String;
      StrList :TStringList;
    begin
      Str := '<刘德华> <张学友> <成吉思汗> <牛顿> <可乐> <斯琴格尔日> <人> <周杰伦> <张会妹> <温造轮>';
      Str := Copy(Str,2,Length(Str)-2);
      StrList := TStringList.Create;
      Str := StringReplace(Str,Delimiter,',',[rfReplaceAll]);
      StrList.CommaText := Str;
      ListBox1.Items := StrList;  //这里测试...
    end;
      

  3.   

    呵呵~~~楼上的比较麻烦...而且前面的'<'和后面的'>'没有去掉...
      

  4.   

    回复人: Kevin_Lmx(繁华阅尽):如果我的"> <"中间的空格处也是不同的字符呢?我这个字符串又长又杂,上面的只是个例子,"> <"中间也有不同的字符,再幸苦一下,可以的话马上给你们分。
      

  5.   

    就此题而言,可以如下处理,如果分隔符‘>’'<'中还有其它字符,则要使用其它方法
    i:integer;
    StrList:Tstrings;
    NameWord:String;
    Str:=<xxx><yyy>
        StrList:=TstringList.create;
        strList.clear;
         NameWord:='';
      
       for i:=1 to Length(Str) do
         begin
            
            if str(i)='>' then          
                  strList.add(NameWord);
           
            NameWord:=NameWord+Str(i);        if str(i)='<' then
                NameWord:='';     
         end;
      

  6.   

    这样将字串中'<''>'之间的值取出给了strList
     如果要显示,例如用MEMO1显示:则加上 Memo1.lines:=StrList;
      

  7.   

    string='98/.7<刘德华> 23<张学友> sdf<成吉思汗> 33<牛顿>ds <可乐> ads<斯琴格尔日> hh<人>,./g <周杰伦>asd4d <张会妹>7m,./ <温造轮>'
    其实我的字符串是跟这个差不多的,
    只不过有个规律就是:
    前面有“<word”--------(这个例子中我写成"<"),
    后面有个"doc>"----------(这个例子中我写成">"),太复杂了,但是有一定的规律,我想还是有解决的办法的。
      

  8.   

    procedure TForm1.Button1Click(Sender: TObject);
    const
      Delimiter1 :String = '>';
      Delimiter2 :String = '<';
    var
      Str :String;
    begin
      Str := '<刘德华>452345345<张学友>FASG<成吉思汗>CVNBCV<牛顿>4534fdgsa<可乐>xcvzwe<斯琴格尔日>256fg<人>dfg<周杰伦>53tg<张会妹>sdaf<温造轮>';
      Str := Copy(Str,2,Length(Str)-2);
      while True do
      begin
        ListBox1.Items.Add(Copy(Str,0,Pos(Delimiter1,Str)-1));
        Str := Copy(Str,Pos(Delimiter2,Str)+1,Length(Str)-Pos(Delimiter2,Str));
        if (Pos(Delimiter1,Str)=0) and (Pos(Delimiter2,Str)=0) then
        begin
          ListBox1.Items.Add(Str);
          Break;
        end;
      end;
    end;
      

  9.   

    我先试试,可以的话给分,CSDN里的热心人真多
      

  10.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
    j,i,n:integer;
    s,s2:string;
    begin
    s:= '<刘德华> <张学友> <成吉思汗> <牛顿> <可乐> <斯琴格尔日> <人> <周杰伦> <张会妹> <温造轮>';
    s2:='';
    j:=Length(s);
    for i:=0 to j do
    begin
    if not (s[i]  in ['<','>'] ) then
      s2:=s2+s[i]
    else
      begin
       if s2<>'' then
          listbox1.Items.Add(s2);
      s2:='';
      end;
    end;
    end;
      

  11.   

    何必如此麻烦,用正则表达式就可以了
    Uses VBScript_RegExp_TLB;
      

  12.   

    VBScript_RegExp_TLB单元由C:\WINNT\system32\vbscript.dll导入。
    其实可搜索“正则表达式+delphi”,即可找到无数文章
      

  13.   

    TO  fjx99(fjx99):
    能用正则的方法解这个题吗?想学习一下呢!
      

  14.   

    写个例子:
    你先uses VBScript_RegExp_TLB;然后在按钮响应中:var
       RegExp1     : RegExp;
    begin
       ...   //把#9、#13、#10替换成空格
       RegExp1 := CoRegExp.Create();
       RegExp1.Pattern    := #9+'|'+#13+'|'+#10;
       RegExp1.IgnoreCase := true;
       RegExp1.Global     := true;
       m_SelectSQL        := RegExp1.Replace(m_SelectSQL,' ');   
       //把多个空格替换为一个空格
       RegExp1.Pattern    := ' +';
       m_SelectSQL        := RegExp1.Replace(m_SelectSQL,' ');   
       RegExp1 := nil;   ...
    End;
      

  15.   

    VBScript_RegExp_TLB.Pas的内容:///////////////////////////////////////////unit VBScript_RegExp_TLB;// ************************************************************************ //
    // WARNING                                                                    
    // -------                                                                    
    // The types declared in this file were generated from data read from a       
    // Type Library. If this type library is explicitly or indirectly (via        
    // another type library referring to this type library) re-imported, or the   
    // 'Refresh' command of the Type Library Editor activated while editing the   
    // Type Library, the contents of this file will be regenerated and all        
    // manual modifications will be lost.                                         
    // ************************************************************************ //// PASTLWTR : $Revision:   1.130  $
    // File generated on 2004-4-28 12:18:01 from Type Library described below.// ************************************************************************  //
    // Type Lib: C:\WINNT\system32\vbscript.dll\2 (1)
    // LIBID: {3F4DACA7-160D-11D2-A8E9-00104B365C9F}
    // LCID: 0
    // Helpfile: 
    // DepndLst: 
    //   (1) v2.0 stdole, (C:\WINNT\system32\STDOLE2.TLB)
    //   (2) v4.0 StdVCL, (C:\WINNT\system32\stdvcl40.dll)
    // ************************************************************************ //
    {$TYPEDADDRESS OFF} // Unit must be compiled without type-checked pointers. 
    {$WARN SYMBOL_PLATFORM OFF}
    {$WRITEABLECONST ON}interfaceuses ActiveX, Classes, Graphics, OleServer, StdVCL, Variants, Windows;
      
    // *********************************************************************//
    // GUIDS declared in the TypeLibrary. Following prefixes are used:        
    //   Type Libraries     : LIBID_xxxx                                      
    //   CoClasses          : CLASS_xxxx                                      
    //   DISPInterfaces     : DIID_xxxx                                       
    //   Non-DISP interfaces: IID_xxxx                                        
    // *********************************************************************//
    const
      // TypeLibrary Major and minor versions
      VBScript_RegExpMajorVersion = 1;
      VBScript_RegExpMinorVersion = 0;  LIBID_VBScript_RegExp: TGUID = '{3F4DACA7-160D-11D2-A8E9-00104B365C9F}';  IID_IRegExp: TGUID = '{3F4DACA0-160D-11D2-A8E9-00104B365C9F}';
      IID_IMatch: TGUID = '{3F4DACA1-160D-11D2-A8E9-00104B365C9F}';
      IID_IMatchCollection: TGUID = '{3F4DACA2-160D-11D2-A8E9-00104B365C9F}';
      CLASS_RegExp: TGUID = '{3F4DACA4-160D-11D2-A8E9-00104B365C9F}';
      CLASS_Match: TGUID = '{3F4DACA5-160D-11D2-A8E9-00104B365C9F}';
      CLASS_MatchCollection: TGUID = '{3F4DACA6-160D-11D2-A8E9-00104B365C9F}';
    type// *********************************************************************//
    // Forward declaration of types defined in TypeLibrary                    
    // *********************************************************************//
      IRegExp = interface;
      IRegExpDisp = dispinterface;
      IMatch = interface;
      IMatchDisp = dispinterface;
      IMatchCollection = interface;
      IMatchCollectionDisp = dispinterface;// *********************************************************************//
    // Declaration of CoClasses defined in Type Library                       
    // (NOTE: Here we map each CoClass to its Default Interface)              
    // *********************************************************************//
      RegExp = IRegExp;
      Match = IMatch;
      MatchCollection = IMatchCollection;
    // *********************************************************************//
    // Interface: IRegExp
    // Flags:     (4560) Hidden Dual NonExtensible OleAutomation Dispatchable
    // GUID:      {3F4DACA0-160D-11D2-A8E9-00104B365C9F}
    // *********************************************************************//
      IRegExp = interface(IDispatch)
        ['{3F4DACA0-160D-11D2-A8E9-00104B365C9F}']
        function  Get_Pattern: WideString; safecall;
        procedure Set_Pattern(const pPattern: WideString); safecall;
        function  Get_IgnoreCase: WordBool; safecall;
        procedure Set_IgnoreCase(pIgnoreCase: WordBool); safecall;
        function  Get_Global: WordBool; safecall;
        procedure Set_Global(pGlobal: WordBool); safecall;
        function  Execute(const sourceString: WideString): IMatchCollection; safecall;
        function  Test(const sourceString: WideString): WordBool; safecall;
        function  Replace(const sourceString: WideString; const replaceString: WideString): WideString; safecall;
        property Pattern: WideString read Get_Pattern write Set_Pattern;
        property IgnoreCase: WordBool read Get_IgnoreCase write Set_IgnoreCase;
        property Global: WordBool read Get_Global write Set_Global;
      end;// *********************************************************************//
    // DispIntf:  IRegExpDisp
    // Flags:     (4560) Hidden Dual NonExtensible OleAutomation Dispatchable
    // GUID:      {3F4DACA0-160D-11D2-A8E9-00104B365C9F}
    // *********************************************************************//
      IRegExpDisp = dispinterface
        ['{3F4DACA0-160D-11D2-A8E9-00104B365C9F}']
        property Pattern: WideString dispid 10001;
        property IgnoreCase: WordBool dispid 10002;
        property Global: WordBool dispid 10003;
        function  Execute(const sourceString: WideString): IMatchCollection; dispid 10004;
        function  Test(const sourceString: WideString): WordBool; dispid 10005;
        function  Replace(const sourceString: WideString; const replaceString: WideString): WideString; dispid 10006;
      end;// *********************************************************************//
    // Interface: IMatch
    // Flags:     (4560) Hidden Dual NonExtensible OleAutomation Dispatchable
    // GUID:      {3F4DACA1-160D-11D2-A8E9-00104B365C9F}
    // *********************************************************************//
      IMatch = interface(IDispatch)
        ['{3F4DACA1-160D-11D2-A8E9-00104B365C9F}']
        function  Get_Value: WideString; safecall;
        function  Get_FirstIndex: Integer; safecall;
        function  Get_Length: Integer; safecall;
        property Value: WideString read Get_Value;
        property FirstIndex: Integer read Get_FirstIndex;
        property Length: Integer read Get_Length;
      end;// *********************************************************************//
    // DispIntf:  IMatchDisp
    // Flags:     (4560) Hidden Dual NonExtensible OleAutomation Dispatchable
    // GUID:      {3F4DACA1-160D-11D2-A8E9-00104B365C9F}
    // *********************************************************************//
      IMatchDisp = dispinterface
        ['{3F4DACA1-160D-11D2-A8E9-00104B365C9F}']
        property Value: WideString readonly dispid 0;
        property FirstIndex: Integer readonly dispid 10001;
        property Length: Integer readonly dispid 10002;
      end;// *********************************************************************//
    // Interface: IMatchCollection
    // Flags:     (4560) Hidden Dual NonExtensible OleAutomation Dispatchable
    // GUID:      {3F4DACA2-160D-11D2-A8E9-00104B365C9F}
    // *********************************************************************//
      IMatchCollection = interface(IDispatch)
        ['{3F4DACA2-160D-11D2-A8E9-00104B365C9F}']
        function  Get_Item(index: Integer): IMatch; safecall;
        function  Get_Count: Integer; safecall;
        function  Get__NewEnum: IUnknown; safecall;
        property Item[index: Integer]: IMatch read Get_Item;
        property Count: Integer read Get_Count;
        property _NewEnum: IUnknown read Get__NewEnum;
      end;
      

  16.   

    接上面
    //////////////////////////// *********************************************************************//
    // DispIntf:  IMatchCollectionDisp
    // Flags:     (4560) Hidden Dual NonExtensible OleAutomation Dispatchable
    // GUID:      {3F4DACA2-160D-11D2-A8E9-00104B365C9F}
    // *********************************************************************//
      IMatchCollectionDisp = dispinterface
        ['{3F4DACA2-160D-11D2-A8E9-00104B365C9F}']
        property Item[index: Integer]: IMatch readonly dispid 10001;
        property Count: Integer readonly dispid 1;
        property _NewEnum: IUnknown readonly dispid -4;
      end;// *********************************************************************//
    // The Class CoRegExp provides a Create and CreateRemote method to          
    // create instances of the default interface IRegExp exposed by              
    // the CoClass RegExp. The functions are intended to be used by             
    // clients wishing to automate the CoClass objects exposed by the         
    // server of this typelibrary.                                            
    // *********************************************************************//
      CoRegExp = class
        class function Create: IRegExp;
        class function CreateRemote(const MachineName: string): IRegExp;
      end;// *********************************************************************//
    // The Class CoMatch provides a Create and CreateRemote method to          
    // create instances of the default interface IMatch exposed by              
    // the CoClass Match. The functions are intended to be used by             
    // clients wishing to automate the CoClass objects exposed by the         
    // server of this typelibrary.                                            
    // *********************************************************************//
      CoMatch = class
        class function Create: IMatch;
        class function CreateRemote(const MachineName: string): IMatch;
      end;// *********************************************************************//
    // The Class CoMatchCollection provides a Create and CreateRemote method to          
    // create instances of the default interface IMatchCollection exposed by              
    // the CoClass MatchCollection. The functions are intended to be used by             
    // clients wishing to automate the CoClass objects exposed by the         
    // server of this typelibrary.                                            
    // *********************************************************************//
      CoMatchCollection = class
        class function Create: IMatchCollection;
        class function CreateRemote(const MachineName: string): IMatchCollection;
      end;implementationuses ComObj;class function CoRegExp.Create: IRegExp;
    begin
      Result := CreateComObject(CLASS_RegExp) as IRegExp;
    end;class function CoRegExp.CreateRemote(const MachineName: string): IRegExp;
    begin
      Result := CreateRemoteComObject(MachineName, CLASS_RegExp) as IRegExp;
    end;class function CoMatch.Create: IMatch;
    begin
      Result := CreateComObject(CLASS_Match) as IMatch;
    end;class function CoMatch.CreateRemote(const MachineName: string): IMatch;
    begin
      Result := CreateRemoteComObject(MachineName, CLASS_Match) as IMatch;
    end;class function CoMatchCollection.Create: IMatchCollection;
    begin
      Result := CreateComObject(CLASS_MatchCollection) as IMatchCollection;
    end;class function CoMatchCollection.CreateRemote(const MachineName: string): IMatchCollection;
    begin
      Result := CreateRemoteComObject(MachineName, CLASS_MatchCollection) as IMatchCollection;
    end;end.
      

  17.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      s , value : String;
      P , Start : PChar;
    begin
      s := '<刘德华>dsfdsf<张学友>sdfsdf<成吉思汗> <牛顿> <可乐> <斯琴格尔日> <人> <周杰伦> <张会妹> <温造轮>';
      P := pointer(s);
      if P <> nil then
      while P^ <> #0 do
      begin
        Start := P;
        if P^='<' then
        begin
          while not (P^ in ['>']) do
            Inc(P);
          SetString(value, Start, p-Start);
          Delete(value, 1, 1);
          ShowMessage(value);   //value就是你要的东西
        end;
        Inc(P);
      end;
    end;
      

  18.   

    或者写成一个函数构造函数
    function a(const str: String):TStringList;
    var
      s , value : String;
      P , Start : PChar;
    begin
      Result := TStringList.Create;
    //  s := '<刘德华>dsfdsf<张学友>sdfsdf<成吉思汗> <牛顿> <可乐> <斯琴格尔日> <人> <周杰伦> <张会妹> <温造轮>';
      P := pointer(str);
      if P <> nil then
      while P^ <> #0 do
      begin
        Start := P;
        if P^='<' then
        begin
          while not (P^ in ['>']) do
            Inc(P);
          SetString(value, Start, p-Start);
          Delete(value, 1, 1);
          Result.Add(value)
        end;
        Inc(P);
      end;
    end;调用
    procedure TForm1.Button1Click(Sender: TObject);
    var
      AA : TStringList;
      i: Integer;
    begin
      AA := TStringList.Create;
      AA := a('<刘德华>dsfdsf<张学友>sdfsdf<成吉思汗> <牛顿> <可乐> <斯琴格尔日> <人> <周杰伦> <张会妹> <温造轮>');
      for i:=0 to AA.Count-1 do
        ShowMessage(AA.Strings[i]);
    end;
      

  19.   

    我要直接截取中间的汉字,把<>符号都去掉,我查过一些函数,都只能定义一个分隔符,有没有可以同时定义前后分隔符的函数,这样就可以只用一个循环就取出来???有没有别的方法??
    ---------->
    先用ansireplacetext(string,'<','') 这样就去掉'<'了,不就变成只有'>'这一个分隔符了,
    呵呵~