在visual basic中的文本框中查找最短,平均和最长的文本长度

时间:2011-04-25 20:34:32

标签: vb.net visual-studio-2008

从文本框中找到最短,平均和最长句子的代码是什么

4 个答案:

答案 0 :(得分:0)

不确定你究竟是在问什么,但无论如何都要开枪。您可以在“。”上拆分文字。然后使用LINQ找出满足这些标准的元素。

答案 1 :(得分:0)

嗯,我想最好的方法是将文本框中的文本拆分为:

Dim sSplitString As String() = TextBox1.Text.Split(New Char() {"."c})

Dim sBigSentence As String = String.Empty
Dim sSmallSentence As String = sSplitString(0)

For Each sVal As String In sSplitString
  'Figure out the big sentence
  If sVal.Length > sBigSentence.Length Then
    sBigSentence = sVal
  End If

  'Figure out the small sentence, you might also need to check and make sure the 
  ' the last value doesn't have a blank string.
  If sVal.Length < sSmallSentence.Length Then
    sSmallSentence = sVal
  End If

  'Average Sentence...
Next

我没有测试过这个,但它应该是这样的。我会让你弄清楚平均句子部分:P

答案 2 :(得分:0)

// Something like this should work, remember you might have to trim
// testing for Max
// this is done in linqpad, hence the .Dump();


string txt = "This is the first sentence. Another one. Short";

var sentences = txt.Split('.').ToList();

var result = from s in sentences
             let max = sentences.Max(st=>st.Length)
             where s.Length == max
             select s;

result.Dump();

答案 3 :(得分:0)

    Dim txtInput As String = "This is a test. This is also a test.  LONGWORDHERE?  This works too!"
    Dim Punctuation As Char() = {"?"c, "!"c, "."c, ";"c}

    Dim Sentences = (From mySent In txtInput.Split(Punctuation))

    Dim Avg As Double = Sentences.Average(Function(w) w.Length)
    Dim Max As Integer = Sentences.Max(Function(w) w.Length)
    Dim Min As Integer = Sentences.Min(Function(w) w.Length)

我认为这是你最好的选择。有很多方法可以给猫皮肤,但我认为它很容易阅读,并且应该相当快,至少,取决于您正在使用的文本的大小。

这仅适用于显示的标点符号,并且在字符数中包含空格。

编辑:您希望获得长度大于零的最小值。那么,那条线就会变成......

Dim Min As Integer = (From mySent In txtInput.Split(Punctuation) Where mySent.Length > 0).Min(Function(w) w.Length)

当你说“完全停止”时,我不知道你的意思。这是回归线吗?