Home All Groups Group Topic Archive Search About

how do i compare two string and get the difference?

Author
19 Jun 2006 8:03 PM
dotnetnoob
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

Author
19 Jun 2006 8:27 PM
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

Show quoteHide quote
> 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
>
Author
19 Jun 2006 8:29 PM
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,
Ahmed
dotnetnoob wrote:
Show quoteHide quote
> 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
Author
20 Jun 2006 1:30 PM
dotnetnoob
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.



Show quoteHide quote
"Ahmed" wrote:

> 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,
> Ahmed
> dotnetnoob wrote:
> > 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
>
>