check a substring

hello,

I have a variable like this:

$hint = “Hint: Hello World - kind regards - monday night”

I want just the full content.
If the content is too long I want the first 20 character after the “Hint”

$hint = ($hint).Substring(5) and if $hint is longer than 20 charaters, then cut the rest off

example: $hint = “Hello World - kind regards”

or

I just want the middle of the content -> from charatert 8 to 20
$hint = ($hint).Substring(5,20) -> but this doesn´t work good

Have anyone an idea for this problem

thanks in advance
Maik

I’m not completely sure if I got what you’re asking for …

I want just the full content.
That would be something like this or not:
$hint
If the content is too long I want the first 20 character after the “Hint”
What means “too long” … anyway that would be this:
$hint.Substring(4,20)
I just want the middle of the content -> from charatert 8 to 20
That would be something like this:
$hint.Substring(8,12)

You might explain more detailed what you need.

Like Olaf, I’m not 100% certain what you’re after. I have assumed you want up to 20 characters of the hint text, but not the 'Hint: ’ text at the start of the string. Using $myString.substring(6,20) will return an error if the string is shorter than 20 characters long so you need to allow for shorter hints.

I would use a regular expression to match 0 - 20 characters after the 'Hint: ’

Breaking down the regex:
The parentheses define a substring
?(lt)name(gt) creates a named capture for the substring that we can reference when accessing the Match object.
. specifies any character
{0,20} specifies match the character 0-20 times.

Edit: posted code on Gist because it contains angled brackets.

Using $myString.substring(6,20) will return an error if the string is shorter than 20 characters long
Without trying to be a smart ass but to be correct - the expression would return an error when the string is shorter than 26 charachters. ;-) :-D

Good catch. I appreciate the correction.