Solved

string compare?

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

685 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