Solved

string compare?

Posted on 2014-10-08
3
249 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
[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
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 34

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

Industry Leaders: 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

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

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