Re: String compare
- Posted by Hawke <mdeland at NWINFO.NET> Sep 10, 1998
- 500 views
Roy Shepherd wrote: > I'm trying to compare two strings. > -- example > object st > st="ABC" > if st="ABC" then puts(1,"OK") end if another option, perhaps until you become more fluent in Euph's syntax, besides the one offered by David (using compare(st,"abc") = 0 or the function solution below: -- function IsSame(object a,object b) return compare(a,b)=0 end function ) another option might be to remember that (in essence) strings are arrays (sequences) of characters. you can walk the array, and you can use the = sign for character comparisons. --constant Mary="Mary had a little piggy, whose fleece was skin" --to find out if a string has a 'p' in it, you can do -- for index = 1 to length(Mary) do if Mary[index] = 'p' then Found=TRUE end if end for now, this is def'nly not the best way to test for string equality, but for some cases, it works well, and it may help you to think with sequences. a hard coded 'compare' function would then look (actually, not at all) like the following (in theory, as an algorithmic ex.): constant Mary1="mary had a little pig" constant Mary2="mary had a little gip" IsSame = FALSE --you need the following test, or the variable index will go out of bounds if length(Mary1) = length(Mary2) then IsSame = TRUE end if index = 1 while IsSame and (index <= length(Mary1)) do if Mary1[index] != Mary2[index] then IsSame=FALSE end if index = index + 1 end while if IsSame then puts(1,"They match\n") else puts(1,"They do not match\n") end if hopefully, this will help you see the slight differences in character versus string equality??? hope so :) enjoy--Hawke'