Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell - $var.ToString(x,y) issue

Tags:

powershell

for a user creation scrip in powershell i'm using textbox object to fill the information of the new user (family name, first name)

I return a value like that:

$TextlabelUsername.text = $Textbox1.text.ToString().Substring(0,5) 

Which apply on a button click.

But using that methode if one of my string value is less then 5 caracters the script return an error that the string is not enough long.

Is there a way to select 5 or less caracters or an other method to process ?

like image 495
lotirthos227 Avatar asked Mar 25 '23 03:03

lotirthos227


1 Answers

Try this:

$str = $Textbox1.text.ToString()
$TextlabelUsername.text = $str.Substring(0, [math]::Min(5, $str.Length))
like image 78
Ansgar Wiechers Avatar answered Apr 06 '23 07:04

Ansgar Wiechers