Satch3000
Satch3000

Reputation: 49422

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 |

So something like:

getTextAftertoCharacter('|',String);

This should return "yes" in the example above.

Has Delphi get a function for this?

Upvotes: 0

Views: 7329

Answers (3)

Tupel
Tupel

Reputation: 318

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.

Upvotes: 1

Sertac Akyuz
Sertac Akyuz

Reputation: 54822

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.

Upvotes: 3

nullptr
nullptr

Reputation: 11058

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.

Upvotes: 9

Related Questions