PowerShell convert string / manipulation: substring etc.

Diese Seite gibt es auch in Deutsch

 In this article I show a few examples for manipulating strings (character strings) 

.ToUpper()

Converts a string to uppercase letters

PS C:\> $("test").ToUpper()
Result:
TEST

.ToLower()

Converts a string into lowercase letters

PS C:\> $("TEST").ToLower()
Result:
test

.Contains()

Tests a string whether a certain string is present

PS C:\> $("TEST").Contains("ES")
Result:
True

.StartsWith()

Tests a string to see if it starts with a certain string.

PS C:\> $("TEST").StartsWith("TE")
Result:
True

.EndsWith()

Tests a string to see if it ends with a specific string.

PS C:\> $("TEST").EndsWith("ST")
Result:
True

.Replace()

Replaces a specified string within a string

PS C:\> $("TEST").Replace("TE","ersetzt")
Result:
ersetztST

.Substring()

Replaces parts of a string based on their position

PS C:\> $("TEST").Substring("1")
Result:
EST

here is another example: start and end position:

PS C:\> $("TEST").Substring("1","2")
Result:
ES
  • 1 ... after the first character
  • 2 ... up to the 2nd character

If only the end position should be used, it looks like this:

 

PS C:\> $("TEST").Substring("","3")
Result:
TES

.TrimStart()

Removes certain characters at the beginning of the string:

PS C:\> $("TEST").TrimStart("TE")
Result:
ST

 

positive Bewertung({{pro_count}})
Rate Post:
{{percentage}} % positive
negative Bewertung({{con_count}})

THANK YOU for your review!


Top articles in this section

PowerShell Log-Files: Logging into a textfile - write to file

PowerShell Log-Files: Logging into a textfile - write to file

created: 2022-05-05 from Bernhard

Log files in PowerShell can be created via the Out-File command, via a custom function, or via PowerShell's built-in Transcript. ... continue reading

Preview PowerShell text file and csv read / write

PowerShell text file and csv read / write

created: 2022-05-02 from Bernhard

PowerShell text file and csv read / write ... continue reading

PowerShell Loops and Array

PowerShell Loops and Array

created: 2022-05-02 from Bernhard

An array stores multiple values, similar to a 2-column table. ... continue reading


Questions / Comments