如何从最小的C#到最大的C#获得字符串的长度

问题描述 投票:0回答:1

底部是txt文件中的readbyline行,单词之间用空格分隔(“”)之间如何从最小到最大的长度?

这是我现在的情况

单击链接

link to the picture

代码

            if (System.IO.File.Exists(textFile))
            {

                using (System.IO.StreamReader file = new System.IO.StreamReader(textFile))
                {
                    int counter = 0;
                    string words; 
                    string[] line;

                    while ((words = file.ReadLine()) != null)
                    {
                        line = Regex.Split(words, " ");
                        foreach (string line_1 in line)
                        {

                            Console.WriteLine(line_1 + " " + line_1.Length);
                        }
                        Console.WriteLine(" ");
                        counter++;
                        Console.WriteLine("Line[{1}]: {0}\n", words, counter);
                    }
                    file.Close();
                    Console.WriteLine(" ");
                    Console.WriteLine("File has [{0}] Lines.", counter);
                }
            } 
c# arrays console streamreader string-length
1个回答
0
投票

使用Linq,您将获得最小和最大长度,如下所示。

var min = words.Split(' ').Min(s => s.Length);
var max = words.Split(' ').Max(s => s.Length);
© www.soinside.com 2019 - 2024. All rights reserved.