How to remove the first n number of characters from a string

Several methods of trimming the first few charachters from a string in powershell

Method 1

The first method is using the 'substring' command, with this command we can remove characters from the beggining of the string, the number placed within the brackets is the number of characters to be removed.


$test = "we shall remove the first letter"
$test.Substring(1)
result = e shall remove the first letter


Method 2

In this method we are actually replacing the text using the ‘replace' command, when we replace with nothing we get the result of removing the first characters


$test = "we shall remove the first word"
$test.Replace("we ","")
result = shall remove the first word


Method 3

This method will remove a range of the same character from the start of the string, the curious thing about this command is that the first letter has to be declared in the brackets, yet it does not seem to work with any characters beyond the first one, if you have any idea why this is please leave a comment


$test = "wwwwwwe shall remove the first row of w's"
$test.TrimStart("w")
result = e shall remove the first row of w's


Comments
// Collect comments ''
Search