尝试在文本框中按字符串查找最长的行(多行)。最长将返回该行的编号,例如:
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));
}
但出于某种原因,它不会显示出来。知道为什么吗?
答案 0 :(得分:1)
您的代码在空格处分割文本框的文本 - 您会得到&#34;单词&#34;。然后你绕过所有线路。不知何故,你在盒子的分割文本的一行内搜索你的盒子的完整文本。这不起作用,除非你的文本框只包含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 :(得分: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));
答案 2 :(得分:0)
使用LINQ可以轻松实现。只需按行长排序,然后使用最后一行,因为最长的行将在最后。
var lines = textBox1.Lines.ToList();
var longest = lines.OrderBy(line => line.Length).Last();
var index = lines.IndexOf(longest);
答案 3 :(得分: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));
}
代码,首先将文本字符串从最长到最短排序然后取与第一个相同的长度,然后我们在文本框中搜索它们的索行