Solved

string compare?

Posted on 2014-10-08
3
242 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 33

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 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