how do i compare two string and get the difference?

G

Guest

i have two strings from xml file

str1 = 800.7415_801.101_8.115_216.12
str2 = 800.7415_801.101_8.115_216.12_217.570

the first stream represent a xml file 800.7415_801.101_8.115_261.12.xml
which hold some value that can only be look up after i compare both string
so i can find where to look up the value of object type 217 and the instance
number 570

if i use string.compare i get -1 which mean str2 is greater than str1...but
i have no idea on what to do next. how do i compare both string and base on
the difference to get the 217 and 570
 
G

GhostInAK

Hello dotnetnoob,

Well, I would start by removing the parts that are the same...
Dim tStringOne As String = 800.7415_801.101_8.115_216.12
Dim tStringTwo as String = 800.7415_801.101_8.115_216.12_217.570
Dim tResult As String = tStringTwo.Replace(tStringOne, String.Empty).Trim

This will make tResult = "_217.570"... You should be able to take it from
there.

-Boo
 
A

Ahmed

Hi,

Just wondering if str2 will always contain str1? If this is the case
then you can do the following:

dim tempString() as string
'Check if str1 is part of str2, if yes, instr returns a positive
value
If InStr(str2, str1) > 0 Then
'str1 is part of str2, we will use the mid and split
methods to extract then
'needed characters.
'In your senario the mid method will return "217.570"
'After using the split method, you will have tempString(0)
= "217" and
' tempString(1) = "570"
tempString = Split(Mid(str2, Len(str1) + 1), ".")
End If


I hope that help you,

Cheers,
 
G

Guest

hi, thank you both. it seem like it is what i need.

str1 basically represent a xml file name and str2 hold where the file is and
the object type and instance number of this object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top