CRLF and Split

This topic contains 8 replies, has 4 voices, and was last updated by Profile photo of Fredrik Kacsmarck Fredrik Kacsmarck 1 week, 6 days ago.

  • Author
    Posts
  • #58492
    Profile photo of Fredrik Kacsmarck
    Fredrik Kacsmarck
    Participant

    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
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    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
    Profile photo of Fredrik Kacsmarck
    Fredrik Kacsmarck
    Participant

    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
      Profile photo of Olaf Soyk
      Olaf Soyk
      Participant

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

  • #58502
    Profile photo of Fredrik Kacsmarck
    Fredrik Kacsmarck
    Participant

    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
      Profile photo of Olaf Soyk
      Olaf Soyk
      Participant

      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
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    You can use -split with proper regex

     D:\> $textSplit = $text -split "[`r`n]+"
     D:\> $textSplit.count
    3
     D:\> $textSplit
    12345
    6789
    
    
  • #58573
    Profile photo of David W Knight
    David W Knight
    Participant

    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
    Profile photo of Fredrik Kacsmarck
    Fredrik Kacsmarck
    Participant

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

    Thanks for the input guys.

You must be logged in to reply to this topic.