Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
PowerShell to Split Strings and return required Sub-strings
Summary
This article explains how PowerShell Split operators help in String Manipulation.
Say you start off with a string: 'Word1:Word2 Word3:Word4 Word5:Word6' and need to split those various "words" with one per line like the folowing:
Format1
Word1
Word2
Word3
Word4
Word5
Word6
Format2
Word1
Word2
Word3:Word4
Word5:Word6
Format3
Word1
Word2
Word3
Word4
Word5:Word6
Solution
PowerShell has several methods which could be used to solve this. This demo focussed specifically on Split / string manipulation without Index and Sub-string Methods. To get Format1 output - the approach is very straight forward
'Word1:Word2 Word3:Word4 Word5:Word6'-split':' -split ' ' |
To get Format2 output - we will use the sub-string like show below
'Word1:Word2 Word3:Word4 Word5:Word6'-split':',2 -split ' ' |
The same applies for Format3 as well
'Word1:Word2 Word3:Word4 Word5:Word6'-split':',3 -split ' ' |
Enjoy PowerShell!