TheSteven
TheSteven

Reputation: 910

AnsiStrIComp fails comparing strings in Delphi 2010

I'm slightly confused and hoping for enlightenment.

I'm using Delphi 2010 for this project and I'm trying to compare 2 strings.

Using the code below fails

if AnsiStrIComp(PAnsiChar(sCatName), PAnsiChar(CatNode.CatName)) = 0 then...

because according to the debugger only the first character of each string is being compared (i.e. if sCatName is "Automobiles", PAnsiChar(sCatName) is "A").

I want to be able to compare strings that may be in different languages, for example English vs Japanese.

In this case I am looking for a match, but I have other functions used for sorting, etc. where I need to know how the strings compare (less than, equal, greater than).

Upvotes: 3

Views: 1842

Answers (2)

Andreas Rejbrand
Andreas Rejbrand

Reputation: 108963

I assume that sCatName and CatNode.CatName are defined as strings (= UnicodeStrings)?. They should be.

There is no need to convert the strings to null-terminated strings! This you (mostly) only need to do when working with the Windows API.

If you want to test equality of two strings, use SameStr(S1, S2) (case sensitive matching) or SameText(S1, S2) (case insensitive matching), or simply S1 = S2 in the first case. All three options return true or false, depending on the strings equality.

If you want to get a numerical value based on the ordinal values of the characters (as in sorting), then use CompareStr(S1, S2) or CompareText(S1, S2). These return a negative integer, zero, or a positive integer.

(You might want to use the Ansi- functions: AnsiSameStr, AnsiSameText, AnsiCompareStr, and AnsiCompareText; these functions will use the current locale. The non Ansi- functions will accept a third, optional parameter, explicitly specifying the locale to use.)

Update

Please read Remy Lebeau's comments regarding the cause of the problem.

Upvotes: 5

mbq
mbq

Reputation: 18628

What about simple sCatName=CatNode.CatName? If they are strings it should work.

Upvotes: 0

Related Questions