如何清洗/验证字符串以将其分配给组件名称?(How to wash/validate a string to assign it to a componentname?)

编程入门 行业动态 更新时间:2024-10-24 11:20:29
如何清洗/验证字符串以将其分配给组件名称?(How to wash/validate a string to assign it to a componentname?)

我有一个列出部门的子菜单。 在这个每个部门的背后有一个行动,他的名字被分配了'actPlan'+ department.name。

现在我意识到这是一个坏主意,因为这个名字可以包含世界上任何奇怪的字符,但action.name不能包含国际字符。 很显然,Delphi IDE本身调用某种方法来验证字符串是否是有效的组件名。 任何人都知道更多关于此?

我也有一个想法使用

Action.name := 'actPlan' + department.departmentID;

代替。 优点是departmentID是一个已知的格式'xxxxx-x'(其中x是1-9),所以我只需要用例如下划线替换' - '。 这里的问题是那些旧的动作名已经存在于个人文本文件中。 如果我突然从使用部门名称更改为ID,这将是例外情况。

我当然可以第一次吃掉这个异常,然后调用一个方法,用正确的数据替换那个文本文件并重新加载它。

所以基本上我搜索最优雅和未来的方法来解决这个:)我使用D2007。

I have a submenu that list departments. Behind this each department have an action who's name is assigned 'actPlan' + department.name.

Now I realize this was a bad idea because the name can contain any strange character in the world but the action.name cannot contain international characters. Obviously Delphi IDE itself call some method to validate if a string is a valid componentname. Anyone know more about this ?

I have also an idea to use

Action.name := 'actPlan' + department.departmentID;

instead. The advantage is that departmentID is a known format, 'xxxxx-x' (where x is 1-9), so I have only to replace '-' with for example underscore. The problem here is that those old actionnames are already persisted in a personal textfile. It will be exceptions if I suddenly change from using departments name to the ID.

I could of course eat the exception first time and then call a method that search replace that textfile with the right data and reload it.

So basically I search the most elegant and futureproof method to solve this :) I use D2007.

最满意答案

使用IsValidIdent函数验证组件名称,该函数仅检查第一个字符是字母还是下划线,以及后续所有字符是字母数字还是下划线。

要创建适合这些规则的字符串,只需删除所有不符合条件的字符,然后添加一个符合条件的字符(如果结果以数字开头)。

这种转变可能会为类似的名字产生相同的结果。 如果这不是你想要的,那么你可以在字符串的末尾添加一些独特的东西,例如根据输入字符串计算的校验和或者你的部门ID。

function MakeValidIdent(const s: string): string; var len: Integer; x: Integer; c: Char; begin SetLength(Result, Length(s)); x := 0; for c in s do if c in ['A'..'Z', 'a'..'z', '0'..'9', '_'] then begin Inc(x); Result[x] := c; end; SetLength(Result, x); if x = 0 then Result := '_' else if Result[1] in ['0'..'9'] then Result := '_' + Result; // Optional uniqueness protection follows. Choose one. Result := Result + IntToStr(Checksum(s)); Result := Result + GetDepartment(s).ID; end;

在Delphi 2009及更高版本中,用调用CharInSet函数替换运算符中的第二个。 (Unicode字符不适用于Delphi集合。)在Delphi 8和更早版本中,将第一个运算符更改为经典for循环并将索引编入s 。

Component names are validated using the IsValidIdent function from SysUtils, which simply checks whether the first character is alphabetic or an underscore and whether all subsequent characters are alphanumeric or an underscore.

To create a string that fits those rules, simply remove any characters that don't qualify, and then add a qualifying character if the result starts with a number.

That transformation might yield the same result for similar names. If that's not something you want, then you can add something unique to the end of the string, such as a checksum computed from the input string, or your department ID.

function MakeValidIdent(const s: string): string; var len: Integer; x: Integer; c: Char; begin SetLength(Result, Length(s)); x := 0; for c in s do if c in ['A'..'Z', 'a'..'z', '0'..'9', '_'] then begin Inc(x); Result[x] := c; end; SetLength(Result, x); if x = 0 then Result := '_' else if Result[1] in ['0'..'9'] then Result := '_' + Result; // Optional uniqueness protection follows. Choose one. Result := Result + IntToStr(Checksum(s)); Result := Result + GetDepartment(s).ID; end;

In Delphi 2009 and later, replace the second two in operators with calls to the CharInSet function. (Unicode characters don't work well with Delphi sets.) In Delphi 8 and earlier, change the first in operator to a classic for loop and index into s.

更多推荐

本文发布于:2023-07-26 19:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1279916.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:将其   字符串   组件   名称   wash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!