在文本框中显示数字的上一个和下一个值

时间:2019-02-08 12:35:42

标签: vb.net

我需要做一个简单的操作。如果我有一些数字,例如:

TextBox1.Text = "7,14,15,19,21,24,29,78"

如何设置第二个文本框的值,以便在第一个文本框中依次显示两个数字,其中两个数字是下一个小于(-1)的整数数字,以及下一个大于(+1)的整数。换句话说,我需要第二个文本框来显示它:

TextBox2.Text= "6,8,13,15,14,16,18,20,22,23,25,28,30,77,79"

1 个答案:

答案 0 :(得分:0)

尝试一下

Dim numArr As New List(Of String)
Dim str As String
numArr.AddRange(TextBox1.Text.Split(","))
'^^^^^
'Spilt the text in the text box into a list
For Each s As String In numArr
     Dim number As Integer
     If Integer.TryParse(s.Trim, number) Then
         str &= (number-1).ToString & "," & (number+1).ToString & ","
     End If
Next
TextBox2.Text = str