一行代码搞定
var result = array.Except(new[] { array.MaxBy(y => y.Length) });
还有一种写法
var result = array.OrderBy(x => x.Length).ToArray()[..^1];
MaxBy这个方法没有喔,你的是.net Code语法?
@何生有二十六画: 至少 .NET 6
@dudu: 老项目,没有这么新
(哭笑)
使用 LINQ
using System.Linq;
...
string[] array = new string[] { "我", "你你", "我爱你" };
var results = from str in array
where str.Length < array.Max(item => item.Length)
select str;
Console.WriteLine(string.Join(",", results));
Console.ReadKey();
···
/// <summary>
/// 移除字符串数组中最长的字符串。
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string[] RmoveMaxlenthStr (string[] str)
{
int index = 0;
int lenth = str[0].Length;
List<string> strlist = new List<string>();
for (int i = 0; i < str.Length; i++)
{
if (str[i].Length > lenth)
{
lenth = str[i].Length;
index = i;
}
strlist.Add(str[i]);
}
strlist.RemoveAt(index);
return strlist.ToArray();
}