Solved

string compare?

Posted on 2014-10-08
3
227 Views
Last Modified: 2014-10-20
Hello,

Would someone please educate me as to the best way to compare strings to validate that they match up to a certain character (that being the last hyphen in the string).

Example:

"IT-plasmid2-licalla-0001T1-1" AND "IT-plasmid2-licalla-0001T1-2" = MATCH
"IT-plasmid2-licalla-0001T1-1" AND "IT-IT TEST-0001T1-10" <> MATCH

TIA,
Andrew
0
Comment
Question by:Ahelbling
3 Comments
 
LVL 22

Assisted Solution

by:rspahitz
rspahitz earned 166 total points
ID: 40369658
In previous versions of VB, there was an INSTRREV command.  I know you can get that though a VB6 import.  It would work like this:

Dim lastHyphen1 As Integer
Dim lastHyphen2 As Integer
lastHyphen1 = INSTRREV(originalText1, "-")
lastHyphen2 = INSTRREV(originalText2, "-")
If lastHyphen1 > 0 And lastHyphen2 > 0 Then
   If lastHyphen1.substring(0, lastHyphen1) = lastHyphen2.substring(0, lastHyphen2) Then
' match
  Else
' not match
  End If
End If

I think there may be a version of INSTRREV similar to IndexOf (like LastIndexOf) in the new .Net
lastHyphen1 = originalText1.LastIndexOf("-")' see if this works
0
 
LVL 32

Assisted Solution

by:it_saige
it_saige earned 167 total points
ID: 40369940
Or you could use Contains with Substring.  This code:
Module Module1
	Private Const Item1 As String = "IT-plasmid2-licalla-0001T1-1"
	Private Const Item2 As String = "IT-plasmid2-licalla-0001T1-2"
	Private Const Item3 As String = "IT-IT TEST-0001T1-10"

	Sub Main()
		Console.WriteLine(Item1.Contains(Item2.Substring(0, Item2.Length - 2)))
		Console.WriteLine(Item2.Contains(Item3.Substring(0, Item3.Length - 2)))
		Console.ReadLine()
	End Sub
End Module

Open in new window


Produces this output:Capture.JPG
-saige-
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 167 total points
ID: 40372250
Dim test1A As String = "IT-plasmid2-licalla-0001T1-1"
Dim test1B As String = "IT-plasmid2-licalla-0001T1-2"
Dim test2A As String = "IT-plasmid2-licalla-0001T1-1"
Dim test2B As String = "IT-IT TEST-0001T1-10"

If test1A.Substring(0, test1A.LastIndexOf("-"c)) = test1A.Substring(0, test1B.LastIndexOf("-"c)) Then
	MessageBox.Show("Match for Test1")
Else
	MessageBox.Show("No match for Test1")
End If

If test1A.Substring(0, test2A.LastIndexOf("-"c)) = test1A.Substring(0, test2B.LastIndexOf("-"c)) Then
	MessageBox.Show("Match for Test2")
Else
	MessageBox.Show("No match for Test2")
End If

Open in new window


The small c at the end of "-"c indicates that you are looking for a character, which is faster than looking for a string.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

8 Experts available now in Live!

Get 1:1 Help Now