Is there a way in PowerShell to trim a string without shortening a word

I look around and can't find anything. I would like to trim the string without shortening the short word. Like an excerpt.

$string = "This is my string"

9 = "This is m"

Ideally, I would like "This is my"

+3
source share
2 answers
$string.Substring(0, $string.IndexOf(" ", 9))

To do what you want, that is, to start with the number of the character that you give it, looks for the next space and shortens the short line there.

+11
source

Do you mean $string.Substring(0,$string.lastindexof(" "))?

+3
source

All Articles