Delphi-SysUtils.Trim不删除最后一个空格(?)字符 [英] Delphi - SysUtils.Trim not deleting last space(?) char

查看:52
本文介绍了Delphi-SysUtils.Trim不删除最后一个空格(?)字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Delphi RIO.我已经用Delphi构建了一个Excel插件(也使用了AddIn Express).我遍历一列以读取单元格值.读取单元格值后,执行TRIM函数.TRIM不会删除最后一个空格.代码段...

  acctName:= Trim(UpperCase(Acctname)); 

在代码之前,AcctName为'ABC Holdings'.TRIM功能后相同.看来Excel在那里添加了某种类型的其他字符.(换行?回车?)摆脱这种情况的最佳方法是什么?有没有一种方法可以让调试器向我显示此变量的十六进制值.我已经尝试了INSPECT和EVALUATE窗口.它们都只显示文本.请注意,我必须小心删除非文本字符,并且某些公司名称带有破折号,逗号,撇号等.

**其他信息-基于Andreas的建议,我添加了以下内容...

  ShowMessage(IntToHex(Ord(Acctname [Acctname.Length])));; 

这将返回'00A0'.所以我想我可以做一个简单的StringReplace ...所以我在Andreas代码之前添加此代码...

  acctName:= StringReplace(acctName,#13,'',[rfReplaceAll]);acctName:= StringReplace(acctName,#10,'',[rfReplaceAll]); 

但是,似乎没有任何改变.ShowMessage仍将"00A0"显示为最后一个字符.为什么StringReplace不删除它?

解决方案

如果您想知道字符串最后一个字符的真实身份,则可以显示其Unicode代码点:

  ShowMessage(IntToHex(Ord(Acctname [Acctname.Length]))). 

或者,您可以使用实用程序来调查剪贴板上的Unicode字符,例如

在这里您看到字符串 test string .由于自Delphi 2009起字符串是Unicode(UTF-16),因此每个字符占用两个字节.对于简单的ASCII字符,这意味着第二个字节为空.我们字符串的ASCII值为 74 65 73 74 20 73 74 72 69 6E 67 .您还可以在( 02A0855C )的上一行中看到我们的字符串对象的引用计数为 1 ,长度为 B (= 11)./p>

Delphi RIO. I have built an Excel PlugIn with Delphi (also using AddIn Express). I iterate through a column to read cell values. After I read the cell value, I do a TRIM function. The TRIM is not deleting the last space. Code Snippet...

acctName := Trim(UpperCase(Acctname));

Before the code, AcctName is 'ABC Holdings '. It is the same AFTER the TRIM function. It appears that Excel has added some type of other char there. (new line?? Carriage return??) What is the best way to get rid of this? Is there a way I can ask the debugger to show me the HEX value for this variable. I have tried the INSPECT and EVALUATE windows. They both just show text. Note that I have to be careful of just deleting NonText characters, and some companies names have dashes, commas, apostrophes, etc.

**Additional Info - Based on Andreas suggestion, I added the following...

ShowMessage(IntToHex(Ord(Acctname[Acctname.Length])));

This comes back with '00A0'. So I am thinking I can just do a simple StringReplace... so I add this BEFORE Andreas code...

 acctName := StringReplace(acctName, #13, '', [rfReplaceAll]);
 acctName := StringReplace(acctName, #10, '', [rfReplaceAll]);

Yet, it appears that nothing has changed. The ShowMessage still shows '00A0' as the last character. Why isn't the StringReplace removing this?

解决方案

If you want to know the true identity of the last character of your string, you can display its Unicode codepoint:

ShowMessage(IntToHex(Ord(Acctname[Acctname.Length]))). 

Or, you can use a utility to investigate the Unicode character on the clipboard, like my own.


Yes, the character in question is U+00A0: NO-BREAK SPACE.

This is like a usual space, but it tells the rendering application not to put a line break at this space. For instance, in Swedish, at least, you want non-breaking spaces in 5 000 kWh.

By default, Trim and TStringHelper.Trim do not remove this kind of whitespace. (They also leave U+2007: FIGURE SPACE and a few other kinds of whitespace.)

The string helper method has an overload which lets you specify the characters to trim. You can use this to include U+00A0:

S.Trim([#$20, #$A0, #$9, #$D, #$A]) // space, nbsp, tab, CR, LF
                                    // (many whitespace characters missing!)

But perhaps an even better solution is to rely on the Unicode characterisation and do

function RealTrimRight(const S: string): string;
var
  i: Integer;
begin
  i := S.Length;
  while (i > 0) and S[i].IsWhiteSpace do
    Dec(i);
  Result := Copy(S, 1, i);
end;

Of course, you can implement similar RealTrimLeft and RealTrim functions.


And of course there are many ways to see the actual string bytes in the debugger. In addition to writing things like Ord(S[S.Length]) in the Evaluate/Modify window (Ctrl+F7), my personal favourite method is to use the Memory window (Ctrl+Alt+E). When this has focus, you can press Ctrl+G and type S[1] to see the actual bytes:

Here you see the string test string. Since strings are Unicode (UTF-16) since Delphi 2009, each character occupies two bytes. For simple ASCII characters, this means that every second byte is null. The ASCII values for our string are 74 65 73 74 20 73 74 72 69 6E 67. You can also see, on the line above (02A0855C) that our string object has reference count 1 and length B (=11).

这篇关于Delphi-SysUtils.Trim不删除最后一个空格(?)字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆