zoukankan      html  css  js  c++  java
  • VC++ 比较两个字符串是否相等,字母大小写相关。

    1、strcmp

          这是用于ANSI标准字符串的函数(如string和char *),此函数接受两个字符串缓冲区做为参数,如果两个字符串是相同的则返回零。否则若第一个传入的字符串的值大于第二个字符串返回值将会大于零,若传入的第一个字符串的值小于第二个字符串返回值将小于零。

    char *ch="翔翔糖糖";
    if(strcmp(ch,"翔翔糖糖")==0)
    {
        //字符串相等
    }
    else
    {
        //字符串不相等
    }

    2、wcscmp

          这个函数是strcmp所对应的Unicode系列的函数,它的使用方法和strcmp相同,它用来比较两个Unicode字符串是否相等(如wstring和wchar_t *)。

    wchar_t *ch=L"翔翔糖糖";
    if(wcscmp(ch,L"翔翔糖糖")==0)
    {
        //字符串相等
    }
    else
    {
        //字符串不相等
    }

    3、 strncmp

    #include    <string.h>
     
    int strncmp(const char * s1, const char * s2, size_t len)
    {
        while(len--) {
            if(*s1 == 0 || *s1 != *s2)
                return *s1 - *s2;
             
            s1++;
            s2++;
        }
        return 0;
    }
    

      

          注:以上所介绍的比较字符串是否相等的函数对于英文来说是要区分大小写的,即使字母都相同但是大小写不同,函数也会认为这两个字符串是不同的。要了解不区分大小写的字符串比较函数请看下面

    4、stricmp

          这是用于ANSI标准字符串的函数(如string和char *),此函数接受两个字符串缓冲区做为参数,如果两个字符串是相同的则返回零,不区分大小写。否则若第一个传入的字符串的值大于第二个字符串返回值将会大于零,若传入的第一个字符串的值小于第二个字符串返回值将小于零。

    char *ch="AbcD";
    if(stricmp(ch,"aBCd")==0)
    {
        //字符串相等
    }
    else
    {
        //字符串不相等
    }

    5、wcsicmp

          这个函数是stricmp所对应的Unicode系列的函数,它的使用方法和stricmp相同,它用来比较两个Unicode字符串是否相等,不区分大小写(如wstring和wchar_t *)。

    wchar_t *ch=L"AbcD";
    if(wcsicmp(ch,L"aBCd")==0)
    {
        //字符串相等
    }
    else
    {
        //字符串不相等
    }

    6、strnicmp (非标准C函数)

    #include	<string.h>
    #include	<ctype.h>
    
    int strnicmp(const char * s1, const char * s2, size_t len)
    {
    	register signed char	r;
    
    	while(len--) {
    		if((r = toupper(*s1) - toupper(*s2)) || *s1 == 0)
    			return r;
    		s1++;
    		s2++;
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    AppWidget应用(四)---PendingIntent 之 getService
    AppWidget应用(三)---PendingIntent 之 getBroadcast
    AppWidget应用(二)---PendingIntent 之 getActivity
    JZOJ 3231. 海明距离
    JZOJ 1422. 猴子摘桃
    JZOJ 1421. 二叉树
    SSLOJ 1318.地铁重组
    SSLOJ 1319.埃雷萨拉斯寻宝
    SSLOJ 1317.灵魂分流药剂
    SSLOJ 1316.血色先锋军
  • 原文地址:https://www.cnblogs.com/kissfu/p/3861022.html
Copyright © 2011-2022 走看看