Solved

string compare?

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

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.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

726 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