CRLF and Split

Welcome Forums General PowerShell Q&A CRLF and Split

This topic contains 8 replies, has 4 voices, and was last updated by

 
Participant
2 years, 3 months ago.

  • Author
    Posts
  • #58492

    Participant
    Points: 364
    Helping Hand
    Rank: Contributor

    It's probably something noobish but I'm wondering why this happens.

    Example Code:

    $text = "12345`r`n6789`r`n"
    
    $textSplit = $text.Split("`r`n")
    
    $textSplit.Length
    

    Why does this genereate a length of 5?

    I would expect this to be 3.
    First Line: 12345
    Second Line: 6789
    Third Line: Empty due to the CRLF at the end.

  • #58495

    Participant
    Points: 1,164
    Helping Hand
    Rank: Community Hero

    Try this:

    $textSplit | Foreach-Object -Process { "'$($_)'" }

    There you will see it's more a count than a length. 😉

    If you use Split("`r`n") the string will be splitted on all `r's AND `n's.

  • #58498

    Participant
    Points: 364
    Helping Hand
    Rank: Contributor

    Yes the textSplit will be a count of lines, the question is why the split generate the empty lines.

    So.
    12345
    "
    6789
    "
    "

    Would expect it to be:
    12345
    6789
    "

    • #58499

      Participant
      Points: 1,164
      Helping Hand
      Rank: Community Hero

      ... If you use Split("`r`n") the string will be splitted on all `r's AND `n's.

  • #58502

    Participant
    Points: 364
    Helping Hand
    Rank: Contributor

    Hmm, ok then I would need to remove the CR from the string first (in the real world scenario).
    Since I don't know if it will be there or not.
    Seems that depending on system/program that is used it's either CR & LF or just LF that get added.

    • #58505

      Participant
      Points: 1,164
      Helping Hand
      Rank: Community Hero

      Hmm, ok then I would need to remove the CR from the string first (in the real world scenario).
      Since I don't know if it will be there or not.

      Sounds like :-/ But as you are aware off this now it's not a big deal. 😉

      Seems that depending on system/program that is used it's either CR & LF or just LF that get added.

      I guess that's a little bit a Windows Unix thing.

  • #58510

    Participant
    Points: 0
    Rank: Member

    You can use -split with proper regex

     D:\> $textSplit = $text -split "[`r`n]+"
     D:\> $textSplit.count
    3
     D:\> $textSplit
    12345
    6789
    
    
  • #58573

    Participant
    Points: 0
    Rank: Member

    The easy solution is to use the '-split' operator, which splits the string on any whitespace, including carriage returns, line feeds, spaces, etc.

    c:\> $text = "12345`r`n6789`r`n"
    c:\> $textSplit = -split $text
    c:\> $textSplit.Length
    2
    c:\> $textSplit
    12345
    6789
    c:\> 
    
  • #58615

    Participant
    Points: 364
    Helping Hand
    Rank: Contributor

    Yep, it seems it's best to use -split and either using the defaults or the earlier regex example.

    Thanks for the input guys.

The topic ‘CRLF and Split’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort