PoshCode Archive  Artifact Content

Artifact 686ebdab8592ce29d824f3b885ac9c64e4598407590a4203e6ed1c6cbfd41ec1:

  • File Split-TextToLines-Demo.ps1 — part of check-in [b9138f2cd3] at 2018-06-10 14:25:53 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: 1277)

# 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: 883
# x-archived: 2016-06-08T10:20:49
# x-published: 2009-02-20T13:08: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