Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get substring until first numeric character

Tags:

vb.net

like my title already explained, I want to get a substring of a string (who contains a address) and I would like to have only the street..

It's not possible to only take the text (non-numeric) chars, because then the box will remain. It's not possible to take substring till first space, because the streetname can contain a space..

For example 'developerstreet 123a' -> would like to have 'developerstreet' The 'a' is a box number of the house, which I'm not interested in..

How can I do this in VB.NET?

like image 459
Yosoyke Avatar asked Aug 17 '26 17:08

Yosoyke


1 Answers

Parsing addresses is notoriously difficult, so I caution you to make sure that you a very deliberate about the choices you make. I would strongly recommend reviewing the documentation provided by the postal service. If these are US addresses, you should start by looking at the USPS Publication 28.

However, to answer your specific question, you can find the index of the first numeric character in a string by using the Char.IsDigit method. You may also want to take a look at the Char.IsNumber method, but that's probably more inclusive than what you really want. For instance, this will get the index of the first numeric character in the input string:

Dim index As Integer = -1
For i As Integer = 0 to input.Length - 1
    If Char.IsDigit(input(i)) Then
        index = i
        Exit For
    End If
Next

However, for complex string parsing, like this, I would suggest learning Regular Expressions. Getting the non-numeric portion at the beginning of a string becomes trivial with RegEx:

Dim m As Match = Regex.Match(input, "^\D+")
If m.Success Then
    Dim nonNumericPart As String = m.Value
End If

Here is the meaning of the regular expression in the above example:

  • ^ - The matching string must start at the beginning of the line
  • \D - Any non-numeric character
  • + - One or more times
like image 180
Steven Doggart Avatar answered Aug 19 '26 22:08

Steven Doggart