Solved

InStr Function

Posted on 2007-04-07
4
592 Views
Last Modified: 2008-02-01
Could you help using these two functions in .net way?

InStr Function
InStrRev Function
0
Comment
Question by:VBdotnet2005
4 Comments
 
LVL 12

Assisted Solution

by:Mamtha1982
Mamtha1982 earned 100 total points
ID: 18871908
use IndexOf and LastIndexOf functions for string objects.

Ex:
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.



0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18872440
If you are converting legacy code, don't forget that most VB6 functions are "one based" while .Net functions are "zero based".
0
 
LVL 27

Accepted Solution

by:
VBRocks earned 150 total points
ID: 18872518
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.


0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 18872724
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.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now