对于文本一行或多行内容读取时的困惑


PowerShell交流中心分类: Powershell基础对于文本一行或多行内容读取时的困惑
1
migou asked 8 年 ago

如一个文本文件test.txt有大于一行以上的内容: Line1 Line2 … (get-content test.txt)[0] 值为Line1,(get-content test.txt)[1] 值为Line2,… ————————————— 如一个文本文件test.txt只有一行内容: Line1 (get-content test.txt)[0] 值为l,(get-content test.txt)[1] 值为i,… 每次处理文本都要多加个行数判断,来区别对待,好烦。是本就有这个问题还是根本我的用法有问题?求解。

铁匠铺的长工 replied 8 年 ago

[System.Array]$texts = get-content test.txt ; $texts[0]

migou replied 8 年 ago

对 这个不错

1 Answers
0
Best Answer
Mooser Lee 管理员 answered 8 年 ago

  这个是期望的: 当有多行的时候,Get-Content返回的结果为字符串数组,每一行是一个元素,(Get-Content .\a.txt)[0]返回的就是第一行的数据。 当有一行的时候,Get-Content返回的结果是一个字符串,不是一个数组。但是字符串本身确实一个字符数组,当你通过索引访问时自然返回的是第i个字符。 所以你的判断是少不了的。    

migou replied 8 年 ago

thx~