开发者

Why doesn't my string get trimmed when I call Trim on it?

I added a space to my string, but Trim does not delete this space. Why 开发者_运维百科not?

str:=tstringlist.create;
str.LoadFromFile(s);
Trim(str.strings[1]);
str.Free;


Trim is a function and does not modify its parameter in-place. You mean to write:

str.strings[1] := Trim(str.strings[1]);


Trim returns modified string instead of changing string you passing into it.

trimmed:= Trim(str.strings[1]);

should work.


In order to make the code work as you want, do this:

str:=tstringlist.create;
str.LoadFromFile(s);
str.strings[1]:= Trim(str.strings[1]); //This line was modified
str.Free;

If you want to save the trimmed string to the file (overriding the file), then do this:

str:=tstringlist.create;
str.LoadFromFile(s);
str.strings[1]:= Trim(str.strings[1]); //This line was modified
str.SaveToFile(s);                     //This line was added
str.Free;


If you want to trim all strings(lines) in the string list do this:

str:=tstringlist.create;
str.LoadFromFile(s);
for i:=0 to str.Count - 1 do
  str.strings[i]:= Trim(str.strings[i]); 
str.Free;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜