Is there a way in PowerShell to truncate a string without cutting short a word
I've been looking around and haven't been able to find anything. I would like to truncate a string without cutti开发者_如何学运维ng a word short. Like an excerpt.
$string = "This is my string"
9 = "This is m"
Ideally would like "This is my"
$string.Substring(0, $string.IndexOf(" ", 9))
Should do what you want, i.e. it starts at the character number you give it, looks for the next space, and cuts the string short there.
Do you mean $string.Substring(0,$string.lastindexof(" "))
?
精彩评论