C#算法题目
已有一个字符串,里面包括多行文本。请写一个函数,能够输出字符串中最长的一行 和最短的一行。如果最长和最短的不止一行,请输出最长和最短的每一行。
求源码呀,我自己写不怎么全
List<string> xxx = "abc\r\nef\r\nb\r\nabc".Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList();//后面那个参数是把空行过滤掉,如果不过滤就RemoveEmptyEntries改为None
int maxLen = xxx.Max(o => o.Length);
int minLen = xxx.Min(o => o.Length);
List<string> MaxRows = xxx.FindAll(o => o.Length == maxLen);
List<string> MinRows = xxx.FindAll(o => o.Length == minLen);
using (var fs = new FileStream(Server.MapPath("~/test.txt"), FileMode.Open))
{
using (var sr = new StreamReader(fs))
{
var array = sr.ReadToEnd()
.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
.OrderBy(t => t.Length);
var min = array.First();
var max = array.Last();
Response.Write("max: " + max + " </br>");
Response.Write("min: " + min + "</br>");
sr.Close();
}
fs.Close();
}