• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

Comparing if two strings are the same

I am comparing two strings read from a text file. I need to see if they are the same, I have tried the following code

Dim Buffer As String
Dim DataString As String
Const filename As String = "c:\ipwin5\tools.ini"
Dim page_size4 As String * 25
Dim page_size5 As String
Dim page_size6 As String
Dim user_selection As String * 25

'read the names of the three user defined sizes from the ini file

Buffer = String$(260, Chr$(13))
Call GetPrivateProfileString("footwear", "page_size4", vbNullString, Buffer, Len(Buffer), filename)
DataString = StripChr(Buffer)
page_size4 = Buffer

Buffer = String$(260, Chr$(13))
Call GetPrivateProfileString("footwear", "page_size5", vbNullString, Buffer, Len(Buffer), filename)
DataString = StripChr(Buffer)
page_size5 = Buffer

Buffer = String$(260, Chr$(13))
Call GetPrivateProfileString("footwear", "page_size6", vbNullString, Buffer, Len(Buffer), filename)
DataString = StripChr(Buffer)
page_size6 = Buffer

 user_selection = Footwear.Combo1.text

MsgBox user_selection
MsgBox page_size4

'Decide which has been selected in the drop down box
If page_size4 = user_selection Then

Dim NewValue As String
'get width1 from the ini file

Buffer = String$(260, Chr$(13))
Call GetPrivateProfileString("footwear", "width1", vbNullString, Buffer, Len(Buffer), filename)
DataString = StripChr(Buffer)
NewValue = Buffer

n23 = NewValue

MsgBox n23
MsgBox "values match"

End If

The values are being read correctly but it falls over on the comparisson comletly ignoring it, IF I change it to if string1 <> string 2 it runs a comparison so I assumn even though the text is the same the software is seeing it as differnet, anyone any ideas?
0
eneate
Asked:
eneate
  • 7
  • 3
  • 3
  • +2
4 Solutions
 
TimCotteeCommented:
Hi eneate,

What does StripChr() function do?

Tim Cottee
0
 
eneateAuthor Commented:
Thanks for the reply, how would I add this? The problem is definitly that the texts are different lengths hence they are being seen as different even though they are actually the same.
0
 
TimCotteeCommented:
TimCottee,

There is nothing to add, you have a function StripChr() which presumably does something. The question is what? I ask because it may have a bearing on what happens here. When you declare strings as fixed length they get padded with Chr(0) now in theory that should mean that both values have the same number of chr(0) at the end of whatever string is stored there. However this may not be the case in actual fact. Or there may be spaces in one string that are not in another. Or the case of the two strings is different. Any or all of these issues will affect that string comparison.

Tim
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>Dim page_size4 As String * 25
>Dim user_selection As String * 25
>If page_size4 = user_selection Then

please try this:
if trim(page_size4) = trim(user_selection) Then
0
 
eneateAuthor Commented:
I use the following declare function to get the info from the text file

Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

and this to write it
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As Any, ByVal lpFileName As String) As Long
0
 
eneateAuthor Commented:
I also use this for the strpchr

Public Function StripChr(ByVal AnyBuffer As String) As String
   StripChr = Left$(AnyBuffer, InStr(AnyBuffer, Chr$(0)) - 1)
End Function
0
 
TimCotteeCommented:
eneate,

And of course AngelIII is right as usual. The simple approach usually works. You do indeed need to check the Trimmed value of each string as it is the padding to the fixed length string that causes a problem. His solution will work for you.

Tim
0
 
eneateAuthor Commented:
Thanks Angel but it is still not seeing the strings as the same, I have tried with with and without the *25
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
can you print out the value of the 2 variables like this:

debug.print "#" & trim(page_size4) & "@"
debug.print "#" & trim(user_selection) & "@"
0
 
eneateAuthor Commented:
All I get in the immediate debug window is

@
#papersize1@
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
which shows that page_size4 has some special character in it; can you run this please:

sub show_ansi ( byval s as string )
  dim i as long
  dim l as long

  l = len(s)
  while i < l
    i = i + 1
    debug.print  asc(mid(s, i, 1)), mid(s,i,1)
  wend

end sub


and call this like:

show_ansi  "#" & trim(page_size4) & "@"

what is the output, please

0
 
EDDYKTCommented:
does look like they are the same to me

?-)

may be you also need to test case insensivite


if ucase(trim(page_size4)) = ucase(trim(user_selection)) Then
0
 
allallallCommented:
Do something like this:

dim l as long
for l=0 to Max(len(str1),len(str1))
    if mid(str1,l,1)<>mid(st2r,l,1)
        MsgBox "Index:" & l  & " Char1: " & mid(str1,l,1) & " Char2: " & mid(str2,l,1)
    endif
next



function Max(l1 as long,l2 as long) as long
if l1>l2 then
     Max=l1
else
     Max=l2
endif
end function
0
 
eneateAuthor Commented:
Hi

Cracked it, it relates to how the code loads the text from the buffer, if I load the text into a text box (that is hidden in the window) then compare the text in the text box to the other text it does the comparison and works fine using the trim code.

Thanks to everyone, I'll split points

eneate
0
 
eneateAuthor Commented:
Just to say after Angel suggested the debug window, it showed me that only the one string was being seen, so when I decided to firat load it into an invisible text box it solved the problem. Thanks for all the help, saved me many more frustrating hours!!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 7
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now