如何通过文本框中的字符串确定哪条线最长

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

试图在文本框中找到最长的字符串(多行)。最长将返回该行的编号,例如:

line 1: good day, world
line 2: good evening world
line 3: good morning, world

在文本框中找到的最长字符串将返回第3行的行号,如MessageBox("MAX: 3")或如果找到相同的字符串则显示多行。

注意:也要计算“”空间。

到目前为止我试过这种方式:

string[] stringArray = textBox1.Text.Split(' ');
var  Maximum = "";
var Minimum = textBox1.Lines[0];
foreach (string line in textBox1.Lines)
{
      int index = line.IndexOf(textBox1.Text);
      if (index > 0)
      { 
          if (Minimum.Length > line.Length)
          {
              Minimum = line;
          }

          if (Maximum.Length < line.Length)
          {
              Maximum = line;

           }
           MessageBox.Show(string.Format("MAX: {0} ", index));
       }

但由于某种原因,它不会显示出来。知道为什么吗?

c# winforms
4个回答
1
投票

你的代码在空格处分割文本框的文本 - 你得到“单词”。然后你绕过所有线路。不知何故,你在盒子的分割文本的一行内搜索你的盒子的全文。这不起作用,除非你的文本框只包含1行而没有任何换行,但整个事情再也没有任何意义了。

使用textBox1.Lines并检查所有行的长度,使用最长的:

int i = 0;
int maxLen = -1;
int maxIndex = - 1;
foreach (var l in textBox1.Lines)
{
    if (l.Length > maxLen)
    {
        maxLen = l.Length;
        maxIndex = i;
    }
    i++;
}
// this will only capture the first line if multiple are same length
// maxLen holds the length of the longest line, maxIndex the index of it

如果你想要它更漂亮,请使用Linq:

您可以使用IEnumerable<T>.Select() - 它有一个重载,它也为您提供索引。你创建一个匿名类型,相应的OrderByDescending(),从它获取Max() value并整理Where()长度有这个最大值 - 你可以缩短它没有匿名类型但我认为这样更清楚会发生什么:

using System;
using System.Linq;

public class Program
{
    public static void Main(string[] args)
    {
        string[] stringArray = new[] { // you get this array from Textbox.Lines
            "one line of text",
            "second line with more text",
            "short line",
            "very long line with text and more text",
            "very long line with text and more text" };

        var sorted = stringArray
            .Select((text, index) => new {Index=index, Text=text, Length=text.Length })
            .OrderByDescending(ano => ano.Length)
            .ToList(); // order by lenght descending
        var maxLength = sorted.Max(ano => ano.Length); // get max length

        var maxOnes = sorted.Where(ano => ano.Length == maxLength);

        foreach (var ano in sorted)
            Console.WriteLine(
                $"{ano.Index} has length {ano.Length} and text of '{ano.Text}'");

        Console.WriteLine(
            $"The longest ones had indexes: {string.Join(",",
                                             maxOnes.Select(ano => ano.Index))}");
        Console.ReadLine();
    }
}

输出:

3 has length 38 and text of 'very long line with text and more text'
4 has length 38 and text of 'very long line with text and more text'
1 has length 26 and text of 'second line with more text'
0 has length 16 and text of 'one line of text'
2 has length 10 and text of 'short line'
The longest ones had indexes: 3,4

1
投票

保持简单,这应该做的工作:

Int32 index = 0;
Int32 maxLength = 0;

String[] lines = textBox1.Lines;

for (Int32 i = 0; i < lines.Length; ++i)
{
    Int32 currLength = lines[i].Length;

    if (currLength > maxLength)
    {
        maxLength = currLength;
        index = i;
    }
}

MessageBox.Show(String.Format("MAX: {0}", index));

或者,使用LINQ和OrderByDescending以及IndexOf,您可以继续使用以下代码:

Int32 index = textBox1.Lines.IndexOf(textBox1.Lines.OrderByDescending(x => x.Length).First());
MessageBox.Show(String.Format("MAX: {0}", index));

0
投票

使用LINQ将使这很容易。只需按行长排序,然后使用最后一行,因为最长的行将在最后。

var lines = textBox1.Lines.ToList();
var longest = lines.OrderBy(line => line.Length).Last();
var index = lines.IndexOf(longest);

0
投票

尝试Orderby和在哪里:

        var stringOrdered = textBox1.Lines.OrderByDescending(t => t.Length);
        var mostLargeStrings = stringOrdered.Where(str=> str.Length == stringOrdered.First().Length);             
        var index = 0;
        foreach(var TXT in mostLargeStrings)
        {
            index = Array.IndexOf(textBox1.Lines, TXT, index+1);
            MessageBox.Show(string.Format("MAX: {0} ",index));
        }

代码,首先将文本字符串从最长到最短排序,然后取与第一个相同长度的文本字符串,然后我们在textbox行中搜索它们的索引

© www.soinside.com 2019 - 2024. All rights reserved.