Solved

InStr Function

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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 …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

732 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