Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
41 lines (38 sloc) 1.13 KB
layout title excerpt permalink tags caterogies published comments
single
Powershell - Using String methods
/2010/12/powershell-using-string-functions.html
powershell
scripting
powershell
true
true

PowerShell uses .NET objects everywhere. .NET objects come with some useful built-in methods. However, for string manipulation you do not need to look for external commands as they are built right into string class.

Here are a couple of useful examples:

"Hello".Length              #5
"Hello".ToLower()           #hello
"Hello".ToUpper()           #HELLO
"Hello".EndsWith('lo')      #True
"Hello".Contains('l')       #True
"Hello".LastIndexOf('l')    #3
"Hello".IndexOf('l')        #2
"Hello".Substring(3)        #lo
"Hello".Substring(3,1)      #l
"Hello".StartsWith('he')    #False
"Hello".toLower().StartsWith('he')      #True
"Hello".Insert(3, "INSERTED")           #HelINSERTEDlo
"Hello".Replace('l', 'x')               #Hexxo
" remove space at ends ".Trim()         #remove space at ends
" remove space at ends ".Trim(' rem')   #ove space at ends
"Server1,Server2,Server3".Split(',')
<#
Server1
Server2
Server3
#>
You can’t perform that action at this time.