Path: utzoo!attcan!uunet!wuarchive!udel!haven!adm!news From: CDCKAB%EMUVM1.BITNET@cunyvm.cuny.edu ( Karl Brendel) Newsgroups: comp.lang.pascal Subject: (R)Comparing strings... Message-ID: <24773@adm.BRL.MIL> Date: 15 Oct 90 13:35:41 GMT Sender: news@adm.BRL.MIL Lines: 47 In article <2203.27170055@cc.nu.oz.au>, v8902058@cc.nu.oz.au "Bernard" wrote: >I'm sorry if this is a trival task, but I would like a function that >compares two strings. I would like to be able to find if a string is >equal to, less than, or greater than another string. I tried writing a >routinue myself, but I just can't seem to do it, so I thought I'd ask. What variety of Pascal are you using? Turbo Pascal can apply equality tests directly to string types. I would expect other Pascals which offer strings to be able to do the same thing, but that may not be the case. A Turbo Pascal function to compare two strings could look like this: type ComparisonType = (ctLessThan,ctEqual,ctGreaterThan); function StrCompare(s1,s2 : string) : ComparisonType; begin if s1 < s2 then StrCompare := ctLessThan else if s1 = s2 then StrCompare := ctEqual else StrCompare := ctGreaterThan; end; An example call to StrCompare: ... if StrCompare(LastName,Names[i]) = ctLessThan then Swap(LastName,Names[i]); ... If you want a case-insensitive comparison, just make each string uppercase before applying the tests. Note: passing the strings by reference rather than by value is generally faster and reduces stack usage, but don't do that for the case-insensitive comparison, or you'll upper-case the original strings. +-------------------------------------------------------------------------+ | Karl Brendel Centers for Disease Control | | Internet: CDCKAB@EMUVM1.BITNET Epidemiology Program Office | | Bitnet: CDCKAB@EMUVM1 Atlanta, GA, USA | | InterLink/RIME: KARL BRENDEL phone 404/639-2709 | | CIS : 73307,3101 fts 236-2709 | | GEnie: K.BRENDEL Home of Epi Info 5.0 | +-------------------------------------------------------------------------+