Scott M. wrote:
Don't know what you are saying here, the loop will produce: "10101" as
desired.
Here is the corrected code (tested) that works like a charm:
Dim x As String = "10AF101-25"
Dim y As String() = x.Split("-")
Dim z As String = ""
Dim i As Integer
For i = 0 To y(0).Length - 1
If IsNumeric(y(0)(i)) Then z &= y(0)(i)
Next
<snip>
Notice that the OP asked for the digits that come before the '-' up to
the first non-digit. As the OP points out, in the example string, the
result should be the digits between "10AF" and "-25", i.e., 101.
Your code returns all the numeric chars up to the '-', just
disregarding the intervening letters, that is, 10101, which is wrong.
Even if that was the case, notice that it's not recommended to build a
string like this, but to use a StringBuilder, instead. Also, accessing
array items inside a loop should be avoided whenever possible. If the
OP had asked for all numeric chars before the '-', then a better method
would be:
Function ExtractDigits(Text As String) As String
Dim S As New System.Text.StringBuilder
For Each C As Char in Text
If C = "-"c Then Exit For
If Char.IsDigit(C) Then S.Append(C)
Next
Return S.ToString
End Function
Regards,
Branco.