【问题标题】:Find the numbers that do not exist in a String查找字符串中不存在的数字
【发布时间】:2018-12-29 19:47:24
【问题描述】:

问题如下:我有一组从1到80的数字。
Textbox1.Text 我有以下数字:

TextBox1.Text = "1,4,5,6,7,8,12,13,14,15,16,17,18,19,20,21,22,23,25,28,29,31,33,
                 34,35,36,37,39,40,41,45,46,47,48,49,51,53,54,55,57,59,60,61,62,
                 63,64,66,67, 68,69,70,71,72,73,75,76,77,78,79,80"

如何在TextBox2.Text 中显示1 到80 中不在TextBox1 中的数字?

TextBox2.Text = "2,3,9,10,11 and so on..."

【问题讨论】:

  • 请告诉我们你做了什么...

标签: vb.net winforms linq textbox


【解决方案1】:

考虑到问题引用了 数字,因此结果值可能会在某处用作实际数字(而不仅仅是数字的字符串表示形式),您可以:

  • List(Of Integer) 中的源字符串中提取值
  • 创建一个List(Of Integer),使用Enumerable.Range初始化与源编号最大值相对应的元素数量
  • 使用Enumerable.Except 过滤使用部分数字的完整列表
  • 如果需要,将生成的 List(Of Integer) 转换为以逗号分隔的值字符串。

注意
我假设源字符串已经过验证。如果不是,请使用Integer.TryParse 而不是Integer.Parse。相关示例代码在this (so very similar) question


Dim BaseNumbers As List(Of Integer) = 
    txtInput.Split(","c).Select(Function(n) Integer.Parse(n)).OrderBy(Function(n) n).ToList()

'Get the List of missing numbers in the source range of values
Dim ResultList As List(Of Integer) = 
    Enumerable.Range(1, BaseNumbers.Max()).Except(BaseNumbers).ToList()

'Convert to string the resulting List
Dim resultString = String.Join(",", ResultList)

【讨论】:

  • 系统无法将字符串转换为-1 维数组。错误(Dim ResultString = String.Join(",", ResultList)
  • 您需要引用System.Linq。根据 VB.Net 版本,您可能需要使用 String.Join(",", ResultList.AsEnumerable())。如果它仍然不适合您,您需要告诉我您使用的是哪个 VB.Net 版本(或 Visual Studio 版本)。
  • Visual Basic 2008
  • 这些方法都可以从 .Net 3.5 版中获得。如前所述,您需要添加对System.LinqSystem.CollectionsSystem.Collections.Generic 的引用。打开Project -> Properties -> References -> Imported Namespaces 并添加缺少的内容。如果你的目标是 .Net 2.0,你也需要改变它,至少升级到 3.5。此 VB.Net 版本非常旧。您会错过 StackOverflow 答案提出的许多解决方案。您需要升级(当然,除非是一些学校作业)。
【解决方案2】:

这里有两个不使用 LINQ 的例子。

这个使用字典和列表:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim n As Integer
    Dim present As New Dictionary(Of Integer, Boolean)
    For Each strValue As String In TextBox1.Text.Split(",".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
        If Integer.TryParse(strValue, n) Then
            present.Add(n, True)
        End If
    Next

    Dim notPresent As New List(Of Integer)
    For i As Integer = 1 To 80
        If Not present.ContainsKey(i) Then
            notPresent.Add(i)
        End If
    Next

    TextBox2.Text = String.Join(",", notPresent.ToArray)
End Sub

这个简单地使用了两个列表:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim n As Integer
    Dim present As New List(Of Integer)
    For Each strValue As String In TextBox1.Text.Split(",".ToCharArray, StringSplitOptions.RemoveEmptyEntries)
        If Integer.TryParse(strValue, n) Then
            present.Add(n)
        End If
    Next
    present.Sort() ' optional; if you need these sorted for something else

    Dim notPresent As New List(Of Integer)
    For i As Integer = 1 To 80
        If present.IndexOf(i) = -1 Then
            notPresent.Add(i)
        End If
    Next

    TextBox2.Text = String.Join(",", notPresent.ToArray)
End Sub

【讨论】:

    猜你喜欢
    • 2014-08-23
    • 2013-10-11
    • 1970-01-01
    • 1970-01-01
    • 2012-04-09
    • 2018-02-13
    • 2011-01-11
    • 1970-01-01
    相关资源
    最近更新 更多