Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算多行文本框中的特定字符串(VB.Net)_Vb.net - Fatal编程技术网

计算多行文本框中的特定字符串(VB.Net)

计算多行文本框中的特定字符串(VB.Net),vb.net,Vb.net,我非常想在每次值以降序>升序出现时计算多行文本框中的值。我试了很多次,但都没有达到应有的效果。(VB.Net) Textbox1.行 2 3 2 2 4 7 7 7 28 28 预期输出:Textbox2.Lines 我所尝试的一切都不起作用 #1 Public Function CountCharacter(ByVal value As String, ByVal ch As Char) As Integer Dim cnt As Integer = 0 Fo

我非常想在每次值以降序>升序出现时计算多行文本框中的值。我试了很多次,但都没有达到应有的效果。(VB.Net)

Textbox1.行

2
3
2
2
4
7
7
7
28
28
预期输出:Textbox2.Lines

我所尝试的一切都不起作用

#1
    Public Function CountCharacter(ByVal value As String, ByVal ch As Char) As Integer
      Dim cnt As Integer = 0
      For Each c As Char In value
        If c = ch Then 
          cnt += 1
        End If
      Next
      Return cnt
    End Function

#2
    Dim a As String = "this is test"
    Dim pattern As String = "t"
    Dim ex As New System.Text.RegularExpressions.Regex(pattern)
    Dim m As System.Text.RegularExpressions.MatchCollection
    m = ex.Matches(a)
    MsgBox(m.Count.ToString())

#3
        Public Shared Function StrCounter(str As String, CountStr As String) As Integer
            Dim Ctr As Integer = 0
            Dim Ptr As Integer = 1
            While InStr(Ptr, str, CountStr) > 0
                Ptr = InStr(Ptr, str, CountStr) + Len(CountStr)
                Ctr += 1
            End While
            Return Ctr
        End Function

您需要记住哪些字符串已经出现,然后对它们进行计数。要做到这一点,您可以使用

Dim dict_strCount As Dictionary(字符串的,整数)=新Dictionary(字符串的,整数的)()
'在输入中的每一行上运行
对于tb_yourTextBox.Text.Lines中的每一行作为字符串
'检查我们是否已经计算了此行中的字符串
如果目录计数包含(行),则
dict_strCount(line)+=1'如果是,则增加该计数
其他的
dict_strCount.Add(第1行)'如果没有,则将其添加到字典中(计数为1)
如果结束
下一个
tb_yourOutputTextBox.Text=String.Empty'清除输出
'按值的升序运行字典中的所有元素
'并输出到输出文本框
在dict_strCount.OrderBy(函数(x)x.Value)中,将每个kvp作为KeyValuePair(字符串,整数)存储
tb_yourOutputTextBox.Text+=kvp.Key&“:”&kvp.Value.ToString()&vbNewLine
下一个

您可以测试它

请参见。
dim grouped=TextBox1.Lines.GroupBy(函数)Integer.Parse(s)).ToDictionary(函数(g)g.Key,函数(g)g.Count())
。然后,
grouped(2)=3
grouped(28)=2
等。从internet复制+粘贴代码不是编程。你应该试着理解代码。例如,“您尝试的内容#1”将计算给定字符串中字符的出现次数,例如
CountCharacter(“aaabbc”,“a”c)
将返回3,因为字符串中有3个
a
#2对正则表达式也是如此。问题是你不想数数字符。@Jimi你假设所有的行都是数字。除此之外,这是一个非常复杂的linq魔法。@FalcoGer我不认为,我在读OP写的东西。实际上,您不需要
Integer.Parse()
,只需对字符串值进行分组即可,无需进行转换。如果OP对这个方法感兴趣的话,这一部分将展示如何使用数字。这是评论,不是回答。@HanimMihai我的错,抱歉。这是c#,它是dotNet框架的一部分。是的。我明白了,谢谢。我没有在标题中说我的错误,我注意到并理解了一些有用的东西,我将努力做到这一点,看看它们是否成功。我注意到这是相似的,我将试着看看他们是否成功。@HanimMihai c#和vb都是dotNet的一部分,因此除了少数例外,代码是非常可互换的。
#1
    Public Function CountCharacter(ByVal value As String, ByVal ch As Char) As Integer
      Dim cnt As Integer = 0
      For Each c As Char In value
        If c = ch Then 
          cnt += 1
        End If
      Next
      Return cnt
    End Function

#2
    Dim a As String = "this is test"
    Dim pattern As String = "t"
    Dim ex As New System.Text.RegularExpressions.Regex(pattern)
    Dim m As System.Text.RegularExpressions.MatchCollection
    m = ex.Matches(a)
    MsgBox(m.Count.ToString())

#3
        Public Shared Function StrCounter(str As String, CountStr As String) As Integer
            Dim Ctr As Integer = 0
            Dim Ptr As Integer = 1
            While InStr(Ptr, str, CountStr) > 0
                Ptr = InStr(Ptr, str, CountStr) + Len(CountStr)
                Ctr += 1
            End While
            Return Ctr
        End Function