No newline in a split variable.

Welcome Forums General PowerShell Q&A No newline in a split variable.

This topic contains 6 replies, has 5 voices, and was last updated by

js
 
Participant
3 months, 3 weeks ago.

  • Author
    Posts
  • #109522

    Participant
    Points: 0
    Rank: Member

    Hi!

    I am trying to do something like this:

    $array = "Hello, every, body"
    $array -split ','

    When i do this, it display it in this way:
    Hello
    Every
    Body

    I want them on ONE line. Any suggestions? This is a problem for other stuff too..

  • #109525

    Participant
    Points: 1
    Rank: Member

    This is just a string though, not an array:

    $array = "Hello, every, body"

    This would be an array:

    $array = "Hello", "every", "body"

    To turn the array back into a string you could do this:

    $array -Join " "
  • #109528

    Participant
    Points: 878
    Helping Hand
    Rank: Major Contributor

    Variable Array is not actually an array here, so you can do a replace with space

    $array -replace ',',''

    if its an array of string on integers you can do it like

    "$array"

    String conversion does the magic.

  • #109534
    js

    Participant
    Points: 319
    Helping Hand
    Rank: Contributor

    -split turns a string into an array. It just displays it as three lines. Maybe you want to replace the commas with spaces?

    PS /Users/js> $array = "Hello,every,body"
    PS /Users/js> $array = $array -split ','
    
    PS /Users/js> $array.count                                                                                       
    3
    PS /Users/js> $array[0]                                                                                          
    Hello
    PS /Users/js> $array[1]                                                                                          
    every
    PS /Users/js> $array[2]                                                                                          
    body
    
    PS /Users/js> $array = "Hello,every,body"
    PS /Users/js> $array = $array -replace ',',' '                                                                   
    PS /Users/js> $array                                                                                             
    Hello every body
    
    
  • #109541

    Participant
    Points: 0
    Rank: Member

    I was just wondering, what if you have $array = "one","two","three", PowerShell display them on three lines. How do you get the output on just one line?

    • #109550
      js

      Participant
      Points: 319
      Helping Hand
      Rank: Contributor
      PS C:\users\superuser> $array -join ' '
      one two three
      
      # or
      PS C:\users\superuser> "$array"
      one two three
      
  • #109549

    Participant
    Points: 324
    Helping Hand
    Rank: Contributor

    What exactly are you trying to accomplish? The array is 3 items, so it's 3 rows. Powershell displays rows on each line horizontally by default. Cmdlets like Format-Table or Format-List manipulate the columns, but everything is still displayed on each row horizontall. If you want to see them vertically, as this the other folks have said, you need to convert the array to a string. Outside of that, you are going against the display behavior in Powershell and modifying format XML files, which is complicated because you are going against default display behavior.

The topic ‘No newline in a split variable.’ is closed to new replies.