powershell slice string 1

powershell slice string

$myString = "helloWorld"

# using the range operator [(start_i)..(end_i)]
$myString[3..8] -join ''  # 'loWorl'
$myString[-4..3] -join ''  # 'orldhello'

# using .Substring(start_i, substring_length)
$myString.Substring(3, 3)  # loWorl
$myString.Substring(1)  # elloWorld

Here is what the above code is Doing:
1. Create a string variable called $myString
2. Using the range operator, we’re getting the characters from index 3 to 8
3. Using the range operator, we’re getting the characters from index -4 to 3
4. Using the .Substring() method, we’re getting the characters from index 3 to 3
5. Using the .Substring() method, we’re getting the characters from index 1 to the end of the string

Similar Posts