DaveWentzel.com            All Things Data

String Operators and Object Methods

Operators

Operator Purpose Example
* repeats a string
PS C:\> "-" * 20
--------------------
PS C:\>
+ string concatenator
PS C:\> "Hello" + "World"
HelloWorld
PS C:\>
-replace replacement function
PS C:\> "Hello World" -replace "Hello","Goodbye"
Goodbye World
PS C:\>
-like wildcard
PS C:\> "Hello" -like "Hell*"
True
PS C:\>
-notlike    
-match is a pattern contained in a string
PS C:\> "Hello World" -match "World"
True
PS C:\>
-notmatch    

 

String Object Methods

Method Purpose Example
CompareTo() compares one string to another
PS C:\> $MyString.CompareTo("ello W")
1
PS C:\>
Contains()    
CopyTo() copies part of a string to another string  
EndsWith()    
Equals()    
IndexOf() returns the first occurence  
LastIndexOf()    
Insert() Inserts one string into another
PS C:\> $MyString = "Hello World"
PS C:\> $MyString.Insert(5,"Dave")
HelloDave World
PS C:\>
PadLeft()    
Remove()    
Replace()    
Split() generates an array  
ToLower()    
ToCharArray()    
Trim()/TrimEnd()/TrimStart()    
Chars() returns the character(s) at the specified position
PS C:\> $MyString = "Hello World"
PS C:\> $MyString.Chars(6)
W
PS C:\>
     

PowerShell Home

Add new comment