Part-1: Text Manipulation in PowerShell using Trim, TrimStart , TrimEnd methods
Part-2: Text Manipulation in PowerShell using .ToLower and .ToUpper method
Part-3: Text Manipulation in PowerShell using .StartsWith() and EndsWith() methods
Hi,
Today we are using .replace and .remove methods to play with the string. Lets start. Today i am using some fake email id as text.
$text = Aman.Dhally@xyz.com
$text.Replace("Aman","Sam")
$text.Replace("."," ")
In the screenshot you can se that all “.” are replaced with the “ “ a black space.
lets assume that we have a text file which contain list of all users email id in our “XYZ” company and we want to remove “@xyz.com” and keep the text before “@xyz.com” .
$text.Replace("@xyz.com"," ")
and here is the our output :) ,showing only the text before “@xyz.com”
Remove
Before doing anything else first lets check the length of our text using Length property. The syntax for remove method is to define to keep the number of characters and remove everything else. Not getting my point.. let ,me show to you.
Our text have 19 characters in it.
$text.Length
$text.Remove(4)
You can see that i mentioned 4 characters to keep and remove everything else. and the output is “Aman” which are exactly 4 characters.
$text.Remove(11)
Now i am keeping 11 characters of the string and removing everything else. and you can see that the output is “Aman.Dhally” which are exactly 11 characters .
there is another example of Remove() method.
$text.Remove(4,7)
Video:
Thanks for reading
Aman Dhally