Solved

InStr Function

Posted on 2007-04-07
4
594 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: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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

740 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