[英]Get first middle and last 10 line on textbox
我在文本框中有文本,我想在文本框中獲取前 10 行中間 10 行和最后 10 行。
下面的示例使用 2 以不提供長樣本數據
樣本數據
1.2
1.44
1.68
1.44
1.44
1.2
1.68
1.68
1.68
1.68
預期產出
1.2
1.44
1.44
1.2
1.68
1.68
我的嘗試
var source = txtProcessData.Lines;
var first = source.Take(2);
var last = source.Skip(source.Length - 2);
txtProcessData.Text = String.Join(Environment.NewLine, first);
但有錯誤:
錯誤 2 參數 2:無法從“System.Collections.Generic.IEnumerable”轉換為“string[]”
注意數據行將始終為偶數,因為我有條件。 我也在樣品上使用 2。
我怎樣才能做到這一點?
您可以嘗試一個簡單的Where
:
var source = txtProcessData.Lines;
var result = source
.Where((value, index) => index <= 1 || // first 2 lines
index >= source.Count - 2 || // last 2 lines
index == source.Count / 2 || // middle 2 lines
index == source.Count / 2 - 1);
// ToArray() - Early versions C# want string[] for string.Join
txtProcessData.Text = String.Join(Environment.NewLine, result.ToArray());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.