Texts with Maximum Length (Part 2)

by Mar 20, 2017

Here is another strategy to make sure a text does not exceed a given length. In contrast to our previous tip, this code will not pad spaces in case the text is shorter than the maximum length:

$text = 'this'
$MaxLength = 10
$CutOff = [Math]::Min($MaxLength, $text.Length)
$text.Substring(0,$CutOff)

Key is the Min() method which determines the smaller of two values.

Twitter This Tip! ReTweet this Tip!