跳至内容

StringCompare

StringCompare

此函数用于比较两个字符串,并以整数形式返回比较结果。

int  StringCompare(
   const string&  string1,                 // the first string in the comparison
   const string&  string2,                 // the second string in the comparison
   bool           case_sensitive=true      // case sensitivity mode selection for the comparison
   );

参数

string1

[输入] 第一个字符串。

string2

[输入] 第二个字符串。

case_sensitive=true

[输入] 大小写敏感模式选择。如果为true,则“A”>“a”。如果为false,则“A”=“a”。默认值为true。

返回值

  • -1(减一),如果string1<string2
  • 0(零),如果string1=string2
  • 1(一),如果string1>string2

注意

字符串按符号逐个比较,符号按照当前代码页的字母顺序进行比较。

示例:

void OnStart()
  {
//--- what is larger - apple or home?
   string s1="Apple";
   string s2="home";

//--- compare case sensitive
   int result1=StringCompare(s1,s2);
   if(result1>0) PrintFormat("Case sensitive comparison: %s > %s",s1,s2);
   else
     {
      if(result1<0)PrintFormat("Case sensitive comparison: %s < %s",s1,s2);
      else PrintFormat("Case sensitive comparison: %s = %s",s1,s2);
     }

//--- compare case-insensitive
   int result2=StringCompare(s1,s2,false);
   if(result2>0) PrintFormat("Case insensitive comparison: %s > %s",s1,s2);
   else
     {
      if(result2<0)PrintFormat("Case insensitive comparison: %s < %s",s1,s2);
      else PrintFormat("Case insensitive comparison: %s = %s",s1,s2);
     }
/* Result:
     Case-sensitive comparison: Apple < home
     Case insensitive comparison: Apple < home
*/
  }

另请参阅

:String类型CharToString()ShortToString()StringToCharArray()StringToShortArray()StringGetCharacter()代码页的使用

最后更新于