C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
PowerShell StringThe PowerShell string is simply an object with a System.String type. It is a datatype that denotes the sequence of characters, either as a literal constant or some kind of variable. A String can be defined in PowerShell by using the single or double-quotes. Both the strings are created of the same System.String object type. Examples:Example1: This example describes how to use the single quotes in a String: PS C:\> $String1='It is a Single Quoted String' PS C:\> $String1 It is a Single Quoted String Example2: This example describes how to use the double quotes in a String: PS C:\> $String2="It is a double Quoted String" PS C:\> $String2 It is a double Quoted String ConcatenationThe concatenation of the string is performed using the plus Sign. Examples:Example1: The following example describes how to concatenate the two string variables: PS C:\> $s1="JAVAT" PS C:\> $s2="POINT" PS C:\> $s1+$s2 The output of the last command in the above example will be displayed as JAVATPOINT Example2: We can also use the join operator to join the string. The following example describes how to use this operator: PS C:\> $s1,$s2 -join "T" The output of this example will be displayed as JAVATPOINT Example3: We can also use the method concat() to concatenate the strings. The following example describes how to use this method: PS C:\> $s1="Power" PS C:\> $s2="Shell" PS C:\> [System.String]::Concat($s1,$s2) The output of this example will also be displayed as PowerShell SubString()The SubString is a method which accepts the two overload arguments and returns a part of the longer string. Both the arguments are numerical values and separated by the comma (,). The left value is that value where you had like to start the SubString. The right value represents the number of characters you had like to move to the right of where you started. Example: The following example skips the first three characters and returns the next seven-character from the given string. PS C:\> $s1="Windows PowerShell" PS C:\> $s1.SubString(3,7) The second command in the above example displays the following output: dows Po String FormattingString formatting is a process to insert some characters or string inside a string. We can format the string by using the -f operator. PS C:\> $s1="Windows PowerShell" PS C:\> $s2="POINT" PS C:\> $formattedString = "{0} {1}...." -f $s1,$s2 PS C:\> $formattedString The last command in the above example displays the following output: Windows PowerShell POINT.... Replace()The replace() method accepts the two arguments and is used to replace the characters in a string. Example: In the following example, we can replace the character x to S in the given string. PS C:\> $s1="Windows Powerxhell" PS C:\> $s1.replace("x","S") The second command in the above example displays the following output: Windows PowerShell
Next TopicPowerShell functions
|