How to compare two EndofLines to two EndofLines

If I have two strings and both of them are two EndofLines, how do I compare them?

If I do a simple str1 = str2 (and those are the two strings) it doesn’t work.

I’m not sure I get what you’re attempting, but try this…

var stringOne as String = str1.ReplaceLineEndings( EndOfLine )
var stringTwo as String = str2.ReplaceLineEndings( EndOfLine )
if stringOne = stringTwo then
  '// Do something here
end if

Browser code. Should work, but untested.

If they don’t match, add a break statement when both variables are populated and use the debugger to assess their contents.

Sorry, but it is really an intellectual question. I know this comparison doesn’t work because of the pair, but I’d like to know how to compare other problem characters.

Such as? Comparison is comparison. Not sure how to help without more specific information.

A string of tab characters.

Are you trying to determine if each string contains the same number of tab characters?

That would be one purpose?
I really am just trying to learn something new.
I don’t know if one has to iterate or if there is another way.
I know ChrByte wouldn’t work of course.

You can iterate over all characters in a string quite easily now:

var stringOne as string = "abcdefg"
for each char as string in stringOne.Characters
  '// Do something with the current character here
1 Like

If you want to implement your own custom equality checks, this method should help:

Public Function IsEqual(str1 as String, str2 as String) As Boolean
  if str1 = str2 then Return true
  if str1.Length <> str2.Length then Return False
  var intMax as Integer = str1.Length
  for intCycle as Integer = 0 to intMax
    if str1.Middle( intCycle, 1 ) <> str2.Middle( intCycle, 1 ) then Return False '// You can break here to check the difference
  Return True
End Function

For my test, here’s the code I used:

var stringOne as string = "abcdefg"
var stringTwo as String = "acdefg"

var result as Boolean = IsEqual( stringOne, stringTwo )

stringTwo = "adcdefg"
result = IsEqual( stringOne, stringTwo )

stringTwo = "abcdefg"
result = IsEqual( stringOne, stringTwo )

The first two tests will report False, and the final test will report True

Thank you. It’s always good seeing other’s code and how different it is.

1 Like

Happy to help.

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.