Вопрос

I have a string

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

I need delphi to get whatever the text is after the |

So something like:

getTextAftertoCharacter('|',String);

This should return "yes" in the example above.

Has Delphi get a function for this?

Это было полезно?

Решение

I'm not aware of a single function that does this. I think the easiest way would be to use Pos and then 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.

Note: this will return the whole contents of str if there is no '|'. Check for Pos('|', str) being non-zero if you need different behavior.

Другие советы

You can use 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. If | is not found, the function returns the 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. LastDelimiter can take more than one delimiter which may be usefull. If no delimiter is found it will return the whole string.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top