PowerShell String

Introduction to String in PowerShell


As we have already discussed that PowerShell is build on .Net Framework. According to the .NET definition of string – “A string is a sequential collection of characters that is used to represent text”.

The PowerShell string is simply an object of System.String type.

How to Define String



Anything mentioned within single quote or double quote considered as string in PowerShell.


Only difference between the two is that strings in double quote support string expansion, while a single quote will only represent literal strings.


Operations on String



To find out what operation can be performed on string, Use Get-member command which will return all methods applicable to string.


Length of String



Length property will return the number of characters in string. 


Split Operations on String



Split operation will divide string into multiple line from where split character is present. Lets take our environment PS module path example,


Apply split operation on ';' character.


Similarly, we can use different split characters to divide the string into the desired output.

Join Operations on String



The concatenation of the string is performed using the plus Sign.


We can also use join operator to concatenate multiple strings.


You can also join the string with the Concat method


Substring Operations on String



Substring command in a string is used to extract the part of the string. It totally works on indexing. In a string, the index starts from 0.


First parameter is starting index and second is number of characters to be returned.

Trim Operations on String



Trim operation in PowerShell is used to trim or remove the space or the specific characters from the beginning and the end of the string. For example,

When you use only Trim() function on the string then it will remove the space
from the beginning and the end of the string.


TrimStart() and TrimEnd() functions, which remove the space or the specific character from the beginning and the end of the string respectively.

Case Operations on String



We can convert the entire string into the Upper case or the Lower case.

Index Operations on String



Index operation in a PowerShell used to find the position of the particular character in the string. Indexing starts at 0. IndexOf() operation finds the first occurrence of the character.


LastIndexOf() finds the last occurrence of the character.

Replace Operations on String



To replace the specific character or set of characters, there is a Replace operation available. This operation is not case sensitive.


You can replace the space with a specific character.


We can replace the word as well.

Contains Operations on String



Contain function when applied on a string, it checks if the specific word exists in the string or not. This function is case sensitive. The output will be TRUE / FALSE.



As function is case sensitive, False returned second time.





Comments

Popular Posts

How to Import and Export Delimited Files, like CSV, in PowerShell

PowerShell Arithmetic Operators

How to generate a GUID in PowerShell