If first line in RichtextBox doesn't contain numbers
I need some kind of code to check if the first line in a richtextbox contains numbers or not.
Something like this:
If Richtextbot1.Lines(0) contains Numbers Then
Goto startbot
Else
End If
Here is an example of my richtextbox lines:
2245 queen st west, ON, M6R2W7
12 RRw Rd, ON
ON, M2N4E3
If the first line contains 4 or more numbers, it will use it. After each loop it will delete line 1. So it repeats for the next line. When my code reaches the 3rd line, and it开发者_开发知识库 doesnt find 4 or more numbers it needs to delete the line.
You could simply iterate through the characters in the line looking for one that is a digit based on the premise that if you find a digit you'll be able to find what you term a 'number.'
Public Function DoesLineContainNumber() As Boolean
For index As Integer = 0 To RichTextBox1.Lines(0).Length - 1
If Char.IsDigit(RichTextBox1.Lines(0)(index)) Then
Return True
End If
Next
Return False
End Function
You could then extract these 'numbers' by parsing the string starting with the first digit found.
Try this
Dim firstLineContainsNumber As Boolean = ContainsNumber(RichTextBox1.Lines(0))
...
Public Function ContainsNumber(input As String) As Boolean
Dim myRegex As New Regex("\d")
Return (myRegex.IsMatch(input))
End Function
Try this, I used matches to check for numbers on the first line and then for each match I added 1 to a count variable:
Dim count As Integer
Dim matches As MatchCollection = Regex.Matches(RichTextBox1.Lines(0), "\d")
For Each m As Match In matches
For Each c As Capture In m.Captures
count += 1
Next
Next
If count >= 4 Then
MsgBox("contains 4 or more numbers")
End If
精彩评论