Convert

SIMIN

Regular
Joined
Mar 10, 2008
Messages
92
Hi,
I want to convert this VB6 code to VB.NET:
Visual Basic:
Private Function StripNullChars(ByVal MyString As String) As String
	Do While Len(MyString) > 0
		If Asc(VB.Right(MyString, 1)) <> 0 Then Exit Do
		MyString = VB.Left(MyString, Len(MyString) - 1)
	Loop 
	StripNullChars = MyString
End Function
However, I don't know what are replacements .NET version of VB.Left and VB.Right?
Please help me.
Thanks.
 
The .SubString(...) method of the string type is the best match, however you may find using .Trim easier as it will strip any character you specify from a string.
 
hi
thanks and many thanks for all your valuable helps :)
I am a little bit confused so my final converted code would be:
Visual Basic:
Private Function StripNullChars(ByVal MyString As String) As String
    StripNullChars = MyString.Trim(vbNull)
End Function
you mean?
but it has error
thanks again :)
 
Hi and thanks :) you are so kind
I just replaced it in my code and it's working, I think so :)

sorry but for final confirm we can say that these 2 codes are the same now?

code1:
Visual Basic:
Private Function StripNullChars(ByVal MyString As String) As String
    Do While Len(MyString) > 0
        If Asc(VB.Right(MyString, 1)) <> 0 Then Exit Do
        MyString = VB.Left(MyString, Len(MyString) - 1)
    Loop
    StripNullChars = MyString
End Function

code2:
Visual Basic:
Private Function StripNullChars(ByVal MyString As String) As String
    StripNullChars = MyString.Trim(Convert.ToChar(0))
End Function
 
Technically the original would only strip nulls from the right end of a string while the second version would strip from the start and end (not that you would expect them on the start).

If you only wanted to remove from the end .TrimEnd() would be a better choice.
 
Back
Top