在dephi中如何去全角空格(加急)

marydan 2008-01-09 11:01:56
在dephi中如何去全角空格 高手快来帮忙
谢谢了
...全文
205 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
Henry.6 2008-01-10
  • 打赏
  • 举报
回复
我还没注意过要去掉全角空格,以后要留心了。
marydan 2008-01-09
  • 打赏
  • 举报
回复
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 谢谢大家了
marydan 2008-01-09
  • 打赏
  • 举报
回复
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 谢谢大家了
pilicat 2008-01-09
  • 打赏
  • 举报
回复
喔,要实现类似于trim的功能?那只能自已写点代码了。
marydan 2008-01-09
  • 打赏
  • 举报
回复
高手门快来啊
marydan 2008-01-09
  • 打赏
  • 举报
回复
只去前后的空格
快来帮忙啊
文字中间的空格不去。
无条件为你 2008-01-09
  • 打赏
  • 举报
回复
核心代码只有一句,无论什么地方出现全角空格都会被删掉。

如果是半角空格(正常的空格)则不处理。简单的写:

showmessage(AnsiReplaceStr(' 你 好 ,我  是中  国 !',' ',''));

最简洁。
brightyang 2008-01-09
  • 打赏
  • 举报
回复
直接用函数
AnsiReplace
无条件为你 2008-01-09
  • 打赏
  • 举报
回复
楼上的方法只能去掉出现空格的第一个地方,如果一句话出现多处全角空格,你的方法就不行了。

其实有一种最简单的方法可能你还没有掌握:

uses StrUtils;

procedure TForm1.Button1Click(Sender: TObject);
var s:widestring;
begin
s:=' 你 好 ,我  是中  国 人!';
s:=AnsiReplaceStr(s,' ','');
showmessage(s)
end;
阿发伯 2008-01-09
  • 打赏
  • 举报
回复
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;
helodd 2008-01-09
  • 打赏
  • 举报
回复
12楼的应正确了吧。。。。StringReplace 就可以了。
无条件为你 2008-01-09
  • 打赏
  • 举报
回复
12楼,请注意楼主在5楼对问题的补充要求。
hongqi162 2008-01-09
  • 打赏
  • 举报
回复
var
s:string;
begin
s:='你好 中 国!';
s:=StringReplace(s,' ','',[rfReplaceAll]);
showmessage( s );
end;
无条件为你 2008-01-09
  • 打赏
  • 举报
回复
只去前后的空格
快来帮忙啊
文字中间的空格不去。

----------------------
那你调用这个过程就可以了:

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;
阿发伯 2008-01-09
  • 打赏
  • 举报
回复

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;

2,498

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧