In VB6, we had InStr. Does that still work in VB.Net?
I want to see if a string, s$, contains "1234".
Printable View
In VB6, we had InStr. Does that still work in VB.Net?
I want to see if a string, s$, contains "1234".
It still works, but there are better options. Look at IndexOf in MSDN, but there are other options, as well. You can use InStr, but you will probably be better off not doing so.
There is also the Contains method of a string object that returns a boolean.
Code:Dim s As String = "Hello"
If s.Contains("Hell") Then
MessageBox.Show("Found It!!!")
End If
Hi DroopyPawn
use the myString.Contains("1234"). Like Shaggy Hiker says there are many other methods like myString.Remove Or myString.Replace etc...
Intellisense is your best friend, use it.
Hope this helps
Just to throw my two cents in - and nothing against Shaggy, who is a good man - but IMHO, I see nothing wrong with using the "legacy" VB functions. MS made these available in .NET, so why not use them? I have seen a lot of arguments against doing this by ".NET purists", but I respectfully disagree.
I have found their availability has helped my transition greatly. To use Instr, Mid, Left, Right, etc. just make sure you have an Imports statement at the top of your module:
Imports Microsoft.VisualBasic
- or, you can use an alias -
Imports VB = Microsoft.VisualBasic
Certain functions (like "Left") will need to be qualified, i.e.:
FirstInit = VB.Left(FullName, 1)
On the other hand, the .NET framework does have a lot to offer that was never available in VB6 (the "Contains" example that bmahler pointed out is just one small example). The more you use VB.NET/200X, the more cool things you'll see and make use of, but I for one would not hesitate to use the old-school functions as well. (As I said, there will be those that disagree with that, some strongly.)
The reason that people say to not use them is that these were merely added to add the ability to upgrade projects from vb6 to .net. There is no guarantee that they will be in all future frameworks so if you continue to use legacy code you can not guarantee that your projects will upgrade in the future. It is best practice to not use those functions wherever possible. If you are upgrading a huge project from vb6 to .net you may not have the time or resources to remove all of those calls, but if you are developing in .net you should not be using legacy code.
In general, I agree with all you have said, and have made similar arguments myself. However, take a look at this thread:Quote:
Originally Posted by BruceG
http://www.vbforums.com/showthread.php?t=508836
I started off saying "go ahead and use the legacy function", but ended up testing the two different techniques, and found that there is actually a benefit to using the .NET technique over the legacy technique. Of course, in reality, nobody would see a difference as minor as that, but it's good to keep in mind that there actually IS a difference. I haven't tested this case, but I would be surprised if it proved to be much different that the example in the thread, since both are cases of parsing strings.
If you look at the libraries and what they are doing it is very similar
The .net String.Contains method calls executes the foloowing code
Whereas the legacy code calls this codeCode:Public Function Contains(ByVal value As String) As Boolean
Return (Me.IndexOf(value, StringComparison.Ordinal) >= 0)
End Function
Public Function IndexOf(ByVal value As String, ByVal comparisonType As StringComparison) As Integer
Return Me.IndexOf(value, 0, Me.Length, comparisonType)
End Function
Public Function IndexOf(ByVal value As String, ByVal startIndex As Integer, ByVal count As Integer, ByVal comparisonType As StringComparison) As Integer
If (value Is Nothing) Then
Throw New ArgumentNullException("value" & ChrW(65533))
End If
If ((startIndex < 0) OrElse (startIndex > Me.Length)) Then
Throw New ArgumentOutOfRangeException("startIndex" & ChrW(65533), Environment.GetResourceString("ArgumentOutOfRange_Index" & ChrW(65533)))
End If
If ((count < 0) OrElse (startIndex > (Me.Length - count))) Then
Throw New ArgumentOutOfRangeException("count" & ChrW(65533), Environment.GetResourceString("ArgumentOutOfRange_Count" & ChrW(65533)))
End If
Select Case comparisonType
Case StringComparison.CurrentCulture
Return CultureInfo.CurrentCulture.CompareInfo.IndexOf(Me, value, startIndex, count, CompareOptions.None)
Case StringComparison.CurrentCultureIgnoreCase
Return CultureInfo.CurrentCulture.CompareInfo.IndexOf(Me, value, startIndex, count, CompareOptions.IgnoreCase)
Case StringComparison.InvariantCulture
Return CultureInfo.InvariantCulture.CompareInfo.IndexOf(Me, value, startIndex, count, CompareOptions.None)
Case StringComparison.InvariantCultureIgnoreCase
Return CultureInfo.InvariantCulture.CompareInfo.IndexOf(Me, value, startIndex, count, CompareOptions.IgnoreCase)
Case StringComparison.Ordinal
Return CultureInfo.InvariantCulture.CompareInfo.IndexOf(Me, value, startIndex, count, CompareOptions.Ordinal)
Case StringComparison.OrdinalIgnoreCase
Return TextInfo.IndexOfStringOrdinalIgnoreCase(Me, value, startIndex, count)
Case Else
Throw New ArgumentException(Environment.GetResourceString("NotSupported_StringComparison" & ChrW(65533)), "comparisonType" & ChrW(65533))
End Select
End Function
Like I stated above the reason that you should not use legacy code is that there is no guarantee as to how long Microsoft will have support for it. Future versions of the framework may not support it anymore at a certain point and for you to upgrade then it will just cause you issues.Code:Public Shared Function InStr(ByVal String1 As String, ByVal String2 As String, <OptionCompare()> Optional ByVal [Compare] As CompareMethod = 0) As Integer
If ([Compare] = CompareMethod.Binary) Then
Return (Strings.InternalInStrBinary(0, String1, String2) + 1)
End If
Return (Strings.InternalInStrText(0, String1, String2) + 1)
End Function
Private Shared Function InternalInStrText(ByVal lStartPos As Integer, ByVal sSrc As String, ByVal sFind As String) As Integer
Dim num2 As Integer
If (Not sSrc Is Nothing) Then
num2 = sSrc.Length
Else
num2 = 0
End If
If ((lStartPos > num2) OrElse (num2 = 0)) Then
Return -1
End If
If ((Not sFind Is Nothing) AndAlso (Not sFind.Length = 0)) Then
Return Utils.GetCultureInfo.CompareInfo.IndexOf(sSrc, sFind, lStartPos, (CompareOptions.IgnoreWidth Or (CompareOptions.IgnoreKanaType Or CompareOptions.IgnoreCase)))
End If
Return lStartPos
End Function