Edit2.Text:=LeftStr(Edit1.Text, 4);
能截取字符串的前4位,包括了其中的空格。能否截取不包括空格的字符串?

解决方案 »

  1.   

    str : string;
    str := Edit1.Text;
    str := StringReplace(str,' ','');Edit2.Text:=LeftStr(str, 4); 
      

  2.   


    function GetStr(str : string; nCount : Integer): string;
    var
      I ,Sum: Integer;
    begin
      if str = '' then
      begin
       Result := '';
       Exit;
      end;
      if Length(str) < nCount then
      begin
        Result := trim(str);
        Exit;
      end;
      Sum := 0;
      for I := 1 to Length(str)-1 do
      begin
        if str[I] <> ' ' then
        begin
          Result := Result + str[I];
          inc(Sum);
          if Sum = nCount then Exit;
        end;
      end;
    end;
      

  3.   


    function GetStr(str : string; nCount : Integer): string;
    var
      I ,Sum: Integer;
    begin
      if str = '' then
      begin
       Result := '';
       Exit;
      end;
      if Length(str) < nCount then
      begin
        Result := trim(str);
        Exit;
      end;
      Sum := 0;
      for I := 1 to Length(str) do
      begin
        if str[I] <> ' ' then
        begin
          Result := Result + str[I];
          inc(Sum);
          if Sum = nCount then Exit;
        end;
      end;
    end;
      

  4.   

    Edit2.Text:=LeftStr(trim(Edit1.Text, 4)); 
      

  5.   

    Edit2.Text:=LeftStr(replace(Edit1.Text,' ',''), 4);
      

  6.   

    Edit2.Text:=LeftStr(trim(Edit1.Text), 4); 
      

  7.   

    Edit2.Text:=LeftStr(trim(Edit1.Text), 4); 或者我推荐给楼主一个很强大的delphi自带的分割字符串的函数ExtracStrings。百度一下,很多资料