Reputation: 141
I need to convert some of the legacy code which is currently in VB6 into C#. I am not able to understand this piece of code. Especially the InStr function, could someone please help me out with this and suggest me it's C# equivalent.
For i = 1 To Len(sString)
sChar = Mid$(sString, i, 1)
iPos = InStr(1, "0123456789", sChar, vbBinaryCompare)
If iPos > 0 Then
sRetStr = sRetStr & sChar
End If
Next i
Upvotes: 1
Views: 171
Reputation: 416121
I'd pare that code down to this:
sRetStr = Regex.Replace(sSTring, "[^0-9]", "");
Upvotes: 4
Reputation: 4534
The InStr finds the (one-based) index of a string in another string. The closest equivalent in the modern .Net string methods is .IndexOf. However, I would replace the code you have with this C# statement.
string sRetStr = (sString.Where((c) => char.IsDigit(c)).ToArray()).ToString();
Upvotes: 4