Powershell String

    
###################################################################################################
### 06 - PowerShell String
###################################################################################################


A String can be defined in PowerShell by using the single or double-quotes.
It is a datatype that denotes the sequence of characters.
The PowerShell string is simply an object with a System.String type.

#------------------------------------------------
# Examples 1: Single quotes in a string

 $string_1='Hello World!'
 $string_1
#------------------------------------------------
# OUTOUT
 Hello World!

 #------------------------------------------------
Examples 2: Double quotes in a string

 $string_2="Hello World!"
 $String_2
Hello World!

#------------------------------------------------
Examples 3: Concatenation the two string variables:

 $string_1="Hello"
 $string_2="World!"
 $string_1+$string_2
HelloWorld!
 $string_1 + $string_2
HelloWorld!
 $string_1+" "+$string_2
Hello World!

#------------------------------------------------
Examples 3: Method concat() to concatenate the strings

 $string_1="Hello"
 $string_2="World!"
 [System.String]::Concat($string_1,$string_2)
HelloWorld!

#------------------------------------------------
 $string_1="Hello"
 $string_2=" World!"
 [System.String]::Concat($string_1,$string_2)
Hello World!

#------------------------------------------------
Examples 3: SubString()

The following example skips the first one character and returns the next three-character from the given string.
 $string_1="Hello World!"
 $string_1.SubString(1,3)
ell

 $string_1.SubString(0,3)
Hel
 $string_1.SubString(1,3)
ell
 $string_1.SubString(2,3)
llo

#------------------------------------------------
Examples 4:String formatting is a process to insert some characters or string inside a string. We can format the string by using the -f operator.

 $string_1="Hello Friend, I love"
 $string_2="Windows PowerShell"
 $string_3="Scripting"
 $string_4 = "Hi, {0} {1}....{2}......" -f $string_1,$string_2,$string_3
 $string_4

 #------------------------------------------------
Examples 5: The replace() method accepts the two arguments and is used to replace the characters in a string.

 $string_1="Hello World!"
 $string_1.replace("o","AA")
HellAA WAArld!

#------------------------------------------------
Return the last 5 characters from a string (RIGHT):
$var = "Hello World"
$length = $var.length
$result = $var.substring($length -5)
$result

#------------------------------------------------
Split the characters from a string:
$var = "HelloWorld.doc"
$var.Split(".")
$var1 = $var.Split(".")
$var1[0]

#------------------------------------------------
# Powershell To Remove Everything Before or After A Character

# Example
$s = 'ravindra.sharma@domain.co.in'
$s.Substring(0, $s.IndexOf('.'))
#---------------------------------------
# OUTPUT
ravindra

#---------------------------------------
# Example
'ravindra.sharma@domain.co.in'.split('.')[0]
#---------------------------------------
# OUTPUT
ravindra