?
Solved

InStr Function

Posted on 2007-04-07
4
Medium Priority
?
597 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 12

Assisted Solution

by:Praveen Kumar
Praveen Kumar earned 400 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 86

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 600 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

765 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