22

I have a string that I want to split up in 2 pieces. The first piece is before the comma (,) and the second piece is all stuff after the comma (including the commas).

I already managed to retrieve the first piece before the comma in the variable $Header, but I don't know how to retrieve the pieces after the first comma in one big string.

$string = "Header text,Text 1,Text 2,Text 3,Text 4,"

$header = $string.Split(',')[0] # $Header = "Header text"

$content = "Text 1,Text 2,Text 3,Text 4," 
# There might be more text then visible here, like say Text 5, Text 6, ..
DarkLite1
  • 13,637
  • 40
  • 117
  • 214

4 Answers4

43

PowerShell's -split operator supports specifying the maximum number of sub-strings to return, i.e. how many sub-strings to return. After the pattern to split on, give the number of strings you want back:

$header,$content = "Header text,Text 1,Text 2,Text 3,Text 4," -split ',',2
DarkLite1
  • 13,637
  • 40
  • 117
  • 214
Aaron Jensen
  • 25,861
  • 15
  • 82
  • 91
3

Try something like :

$Content=$String.Split([string[]]"$Header,", [StringSplitOptions]"None")[1]

As you split according to a String, you are using a different signature of the function split.

The basic use needs only 1 argument, a separator character (more info about it can be found here, for instance). However, to use strings, the signature is the following :

System.String[] Split(String[] separator, StringSplitOptions options)

This is why you have to cast your string as an array of string. We use the None option in this case, but you can find the other options available in the split documentation.

Finally, as the value of $Heasder, is at the beggining of your $String, you need to catch the 2nd member of the resulting array.

Aserre
  • 4,916
  • 5
  • 33
  • 56
  • Thanks for your help Ploutox, I tried your suggestion with the example above, but the variable `$Content` stays empty. Very strange.. – DarkLite1 Aug 19 '14 at 12:36
  • @DarkLite1 sorry I didn't try my answer before posting. Splitting with strings is a bit different than splitting with chars. I'll edit my answer acordingly – Aserre Aug 19 '14 at 12:53
  • 2
    Thank you, in the meantime I've found on the help of Microsoft the following `($String -split ",",2)[1]` which seems to work fine for my needs. – DarkLite1 Aug 19 '14 at 13:09
1

method of Aaron is the best, but i propose my solution

$array="Header text,Text 1,Text 2,Text 3,Text 4," -split ','
$array[0],($array[1..($array.Length -1)] -join ",")
Esperento57
  • 16,521
  • 3
  • 39
  • 45
0

This alternate solution makes use of PowerShell's ability to distribute arrays to multiple variables with a single assignment. Note, however, that the -split operator splits on every comma and PowerShell's built-in conversion from Array back to String results in the elements being concatenated back together. So it's not as efficient as String.Split, but in your example, it's negligible.

$OFS = ','
$Content = 'Header text,Text 1,Text 2,Text 3,Text 4,'

[String]$Header,[String]$Rest = $Content -split $OFS

$OFS = ' '
Write-Host "Header = $Header"
Write-Host "Rest   = $Rest"

Finally, $OFS is a special variable in PowerShell that determines which character will be used when joining the array elements back into a single string. By default, it's a space. But it can be changed to anything.

Josh
  • 68,005
  • 14
  • 144
  • 156