在dephi中如何去全角空格 高手快来帮忙 
谢谢了

解决方案 »

  1.   

    var
      s: string;
      i: Integer;
    begin
      s := ' 你好!';
      i := Pos(#161, s);
      if (i <> 0) and (s[i + 1] = #161) then
        Delete(s, i, 2);
      ShowMessage(s);
    end;
      

  2.   

    楼上的方法只能去掉出现空格的第一个地方,如果一句话出现多处全角空格,你的方法就不行了。其实有一种最简单的方法可能你还没有掌握:uses StrUtils;procedure TForm1.Button1Click(Sender: TObject);
    var s:widestring;
    begin
    s:=' 你 好 ,我  是中  国 人!';
    s:=AnsiReplaceStr(s,' ','');
    showmessage(s)
    end;
      

  3.   

    核心代码只有一句,无论什么地方出现全角空格都会被删掉。如果是半角空格(正常的空格)则不处理。简单的写:showmessage(AnsiReplaceStr(' 你 好 ,我  是中  国 !',' ','')); 最简洁。
      

  4.   

    喔,要实现类似于trim的功能?那只能自已写点代码了。
      

  5.   

     function QTrim(const S: String): String;
    var
      I, L: Integer;
    begin
      L := Length(S);
      I := 1;  while (I <= L) and (S[I] <=' ') do Inc(I);
      if I > L then Result := '' else
      begin
        while S[L] <= ' '  do Dec(L);
        Result := Copy(S, I, L - I + 1);
      end;
    end;帮着改改呗。初学DEPHI 谢谢大家了
      

  6.   

     function QTrim(const S: String): String;
    var
      I, L: Integer;
    begin
      L := Length(S);
      I := 1;  while (I <= L) and (S[I] <=' ') do Inc(I);
      if I > L then Result := '' else
      begin
        while S[L] <= ' '  do Dec(L);
        Result := Copy(S, I, L - I + 1);
      end;
    end;帮着改改呗。初学DEPHI 谢谢大家了
      

  7.   


    var
      i, n: Integer;
      s: string;
    begin
      s := ' 你好 世界 ';
      n := Length(s);
      i := 1;
      // 去头
      while (i < n) and (s[i] = #161) do
      begin
        if s[i + 1] = #161 then
        begin
          Delete(s, i, 2);
          Dec(n, 2);
        end
        else
          Inc(i);
      end;
      i := Length(s);
      // 去尾
      while (i > 1) and (s[i] = #161) do
      begin
        if s[i - 1] = #161 then
        begin
          Delete(s, i - 1, 2);
          Dec(i);
        end;
        Dec(i);
      end;
      ShowMessage(s);
    end;
      

  8.   

    只去前后的空格 
    快来帮忙啊 
    文字中间的空格不去。----------------------
    那你调用这个过程就可以了:uses StrUtils;function   QTrim(const   S:   String):   String;
    var tmp:WideString;
    begin
     tmp:=s;
     while AnsiStartsText(' ',tmp) do tmp:=copy(tmp,2,length(tmp));
     while AnsiEndsText(' ',tmp) do tmp:=copy(tmp,1,length(tmp)-1);
     QTrim:=tmp;
    end;
      

  9.   

    var
      s:string;
    begin
      s:='你好 中 国!';
      s:=StringReplace(s,' ','',[rfReplaceAll]);
      showmessage( s );
    end;
      

  10.   

    12楼的应正确了吧。StringReplace 就可以了。