How do I access a string(index) in .Net 1.1?
I have converted this existing C# .NET 2.0 code (which appears to take in a string with any characters and return only the numbers in that string):
private static string StripMIN(string min)
{
string result = string.Empty;
int digit = 0;
for (int i = 0; i < min.Length; i++)
{
if (int.TryParse(min[i].ToString(), out digit) == true)
{
result += min[i];
}
开发者_Go百科 }
return result;
}
to this VB.Net .Net 1.1 code:
Private Function StripMIN(ByVal min As String) As String
Dim result As String = String.Empty
Dim digit As Integer = 0
Dim i As Integer = 0
While i < min.Length
Me.SystemMessages.Text = "Char at " + i + " : " + min(i)
If TryParseInteger(min(i).ToString(), digit) = True Then
result += min(i)
End If
System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
End While
Return result
End Function
I am getting an error message on line 6 of the VB.Net code at min(i)
. The error message reads:
"Expression is not an array or a method, and cannot have an argument list"
I am not very well versed in .Net 1.1 and cannot find an alternative solution to solving this error. Does anybody have any suggestions on how I can access each character of this string and verify that its a number and return only the numbers of the string in .Net 1.1?
Your issue is converting to VB, not a framework change. Try using min.Chars(i)
. See this MSDN article, which shows that the indexer property (where you see min[i]
in C#) is named Chars
in VB)
However, I would use something like this instead:
Private Function StripMIN(ByVal min As String) As String
Dim result as New StringBuilder()
ForEach c In min
If Char.IsDigit(c) Then
result.Append(c)
End If
End ForEach
Return result.ToString()
End Function
The default indexer min[x]
in C# corresponds to the named indexer min.Chars(x)
in VB.
However, you don't need to loop the characters in the string. You can use a regular expression that matches all non-digit characters to remove them:
Private Function StripMIN(ByVal min As String) As String
Return Regex.Replace(min, "[^\d]+", String.Empty)
End Function
This example shows three ways to extract a character from a string in VB2010 .NET 4.0. I don't have .NET 1.1 as a testbed, but the .Char( ) and .Substring( ) show up in VB2003 documentation. The first method doesn't seem to be documented for the string class but it seems to work without problems in this version. (The variable on the left doesn't have to be Char data type.)
Dim chrCurrentCharacter As Char
Dim strtemp As String = "abc"
For counter As Integer = 0 To strtemp.Length
chrCurrentCharacter = strtemp(counter)
chrCurrentCharacter = strtemp.Chars(counter)
chrCurrentCharacter = CChar(strtemp.Substring(counter, 1))
Next
You should try to use the Substring method on the variable of min. Something like this:
Me.SystemMessages.Text = "Char at " + i + " : " + min.Substring(i, 1)
I think that is correct. Give it a try and let me know what you find. I'm used to C#
精彩评论