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 str开发者_开发百科ings.
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).
I assume that sCatName
and CatNode.CatName
are defined as string
s (= UnicodeString
s)?. 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.
What about simple sCatName=CatNode.CatName
? If they are strings
it should work.
精彩评论