• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 601
  • Last Modified:

InStr Function

Could you help using these two functions in .net way?

InStr Function
InStrRev Function
2 Solutions
Praveen KumarArchitectCommented:
use IndexOf and LastIndexOf functions for string objects.

Dim s As String = "Wel Come"
Dim i As Integer

i=s.IndexOf("e") 'stores 1 into i
i=s.IndexOf("C") 'stored 4 into i
i=s.IndexOf("xx") 'stored -1 into i, because xx is not found

LastIndexOf function will return the index position, but it will search from end.

Mike TomlinsonMiddle School Assistant TeacherCommented:
If you are converting legacy code, don't forget that most VB6 functions are "one based" while .Net functions are "zero based".
Mamtha1982 is right, and all credit to him, the Instr and InStrRev functions are still supported in .NET, but they are not the official way to handle strings.  If it's ok, I would just like to elaborate a little for your better understanding.

The IndexOf and LastIndexOf functions are available for every string in .NET.  So, for example:
Dim i as Integer

'Use the "IndexOf" method with a regular string.
i = "My String".IndexOf("String")

'Use the "IndexOf" method with a string variable.
Dim str as String = "This is another string"
i = str.IndexOf("string")

'Use the "IndexOf" method with a TextBox, because the "Text" property is a String.
Dim tb As New TextBox
tb.Text = "my string"
i = tb.Text.IndexOf("string")

'Here is a LastIndexOf example for you:
Dim strFilePath As String = "C:\Temp\MyFile.txt"
Dim i As Integer
i = strFilePath.LastIndexOf("\")
     ' i now equals the integer 7:  the exact position of the last "\" (see notes below).

** Only 2 points to be aware of as you start using IndexOf and LastIndexOf:
1.  If no match is found, then a -1 is returned, not a 0 as with the Instr and InstrRev functions.
          'Here, a -1 will be returned, because the question mark is not found.
          i = "My String".IndexOf("?")

2.  Strings are zero (0) based in .NET.
          'So for example, the integer "3" will be returned,
          '      because the first character in the string "M" is position 0.
          i = "My String".IndexOf("String")

One final note:
'If you want to search for an exact match, then use the default,
i = "My String".IndexOf("String")

'However, if you want to search for a string, but you want to ignore the case
'such as UPPER CASE, Mixed Case, and lower case, then make sure you use the following:
i = "My String".IndexOf("string", StringComparison.CurrentCultureIgnoreCase)

So, in summary:
-  If you used the Instr method in VB6, use the IndexOf method in .NET.
-  If you used the InstrRev method in VB6, use the LastIndexOf method in .NET.

Jeff CertainCommented:
To elaborate on what VBRocks said, there is an entire library for VB6 compatibility. You just need to import Microsoft.VisualBasic to get Left, Right, Mid, and the rest of the string functions.

However, this namespace does not include DAO, ADO or RDO (the old VB6 data approaches). You'll either need to bring those in as their own libraries, or move to ADO .NET. Neither of these options is painless.

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now