简体   繁体   English

Delphi在定义的字符后搜索文本

[英]Delphi search for text after a defined character

I have a string 我有一串

String :='this is my string | yes';

I need delphi to get whatever the text is after the | 我需要delphi来获得|之后的任何文本。

So something like: 所以像这样:

getTextAftertoCharacter('|',String);

This should return "yes" in the example above. 在上面的示例中,它应该返回“ yes”。

Has Delphi get a function for this? Delphi有为此功能吗?

I'm not aware of a single function that does this. 我不知道执行此操作的单个功能。 I think the easiest way would be to use Pos and then Copy : 我认为最简单的方法是使用Pos然后Copy

answer := Copy(str, Pos('|', str) + 1, Length(str));

Start character is at Pos('|', str) + 1 , amount of characters to copy is actually Length(str)-Pos('|', str) , but passing a greater value to Copy also works. 起始字符位于Pos('|', str) + 1 ,要复制的字符数实际上是Length(str)-Pos('|', str) ,但是将较大的值传递给Copy也可以。

Note: this will return the whole contents of str if there is no '|' 注意:如果没有'|'它将返回str的全部内容。 . Check for Pos('|', str) being non-zero if you need different behavior. 如果需要其他行为Pos('|', str)请检查Pos('|', str)是否为非零。

You can use Controls.GetLongHint() : 您可以使用Controls.GetLongHint()

GetLongHint(String);

That's because the separator of a two part hint is a | 这是因为两部分提示的分隔符是| . In your example it would return ' yes', with the leading space. 在您的示例中,它将以空格开头返回“ yes”。 If | 如果| is not found, the function returns the String . 找不到,该函数返回String

I'd use then following code: 我会用下面的代码:

Trim(RightStr(Text, Length(Text)-LastDelimiter('|', Text)));

It'll locate the last delimiter in the string and I used Trim to get rid of the whitespace. 它会找到字符串中的最后一个定界符,并且我使用Trim摆脱了空格。 LastDelimiter can take more than one delimiter which may be usefull. LastDelimiter可以使用多个定界符,这可能很有用。 If no delimiter is found it will return the whole string. 如果找不到分隔符,它将返回整个字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM