PoshCode Archive  Artifact [7b6eaf66ef]

Artifact 7b6eaf66efd412e05b569ed71662f3720834b64c9f4e84e481f44506e164e9a9:

  • File Split-TextToLines-Demo.ps1 — part of check-in [274fa9ed3c] at 2018-06-10 14:25:59 on branch trunk — Demo of different ways to split a text into lines. Sorry I have not yet another nice way to post code in my blog. http://pauerschell.blogspot.com/. Understanding TextToLines splitting is basic to ISE-Extensions. (user: Bernd Kriszio size: 1558)

# encoding: ascii
# api: powershell
# title: Split-TextToLines Demo
# description: Demo of different ways to split a text into lines. Sorry I have not yet another nice way to post code in my blog. http://pauerschell.blogspot.com/. Understanding TextToLines splitting is basic to ISE-Extensions.
# version: 0.1
# type: function
# author: Bernd Kriszio
# license: CC0
# function: Show-LineArrayStructure
# x-poshcode-id: 886
# x-derived-from-id: 3201
# x-archived: 2016-07-08T10:32:20
# x-published: 2009-02-21T10:31:00
#
#
function Show-LineArrayStructure ($lines)
{
    $len = $lines.length
    "Type is:         $($lines.gettype().Name)"
    "Number of lines: $len"
    for ($i = 0; $i -lt $len; $i++)
    {
        "$($i + 1). Line: length $($lines[$i].length) >$($lines[$i])<"
    }
    '' 
    
} 


    $text = "abc`r`nefg`r`n"


#     $text
#     $text.length

    $lines = $text.Split("`n")
    Show-LineArrayStructure $lines
    

    $lines2 = $text.Split("`r`n")
    Show-LineArrayStructure $lines2

    $lines3 = $([Regex]::Split($text,"`r`n" ))
    Show-LineArrayStructure $lines3
    
    $lines4 = $text -split "`n"
    Show-LineArrayStructure $lines4

   # best method
   $lines5 = $text -split "`r`n"
    Show-LineArrayStructure $lines5

    # even better proposed by Jaycul in comment on
    # http://pauerschell.blogspot.com/2009/02/ise-extension-basics-splitting-text-to.html
    $lines6 =$text.Split([string[]]"`r`n", [StringSplitOptions]::None)
    Show-LineArrayStructure $lines6