PowerShell Tip: Validating String
Summary
This TechNet wiki is to show a simple tricks to validate the strings using PowerShell.
Convert to Upper and Lower case
The give string is 'PowerShell'
'PowerShell' | GM |
TypeName: System.String
'PowerShell'. ToUpper ( ) |
'PowerShell'. ToLower ( ) |
Search Character in the String
[char]$search = Read-Host 'Enter a character' 'PowerShell'. Contains ($search ) |
Or alternative we can use -match operator
'PowerShell' -match 'P' |
Select first 5 characters
'PowerShell'. ToCharArray (0,5 ) -join '' |
Syntax: 'String'.ToCharArray(Int StartIndex, Int Length). 0 is start index and required lenght is 5.
Remove White Space at the End
'PowerShell '. TrimEnd ( ) |
Remove Characters from the String
The below code will remove first 3 characters from the given string.
'PowerShell'. Remove (0,3 ) |
To remove last 3 characters we need to use sub string
$string = 'PowerShell' $string. Substring (0,$string. Length-3 ) |
Other Languages
This article is also available in the following languages: