关于字符串操作的一些函数简单说明与应用

1 strcasecmp, strncasecmp

此函数类似与 strcmp,比较字串符,只是对大小写不敏感(不区分)

int main (void )                                                                                                                                  
{                                                                                                                                                 
    int retval = -1;                                                                                                                              
                                                                                                                                                  
    const char *psrc = "Hello,World!";                                                                                                            
                                                                                                                                                  
//  const char *pdst = "hello,World!";                                                                                                            
    const char *pdst = "Hello,World!";                                                                                                            
                                                                                                                                                  
    size_t sSrcLen = strlen( psrc );  

/* NAME                                                                                                                                           
        strcmp, strncmp - compare two strings                                                                                                     
 * SYNOPSIS                                                                                                                                       
        #include <string.h>                                                                                                                       
        int strcmp(const char *s1, const char *s2);                                                                                               
        int strncmp(const char *s1, const char *s2, size_t n);                                                                                    
 * DESCRIPTION                                                                                                                                    
        The  strcmp()  function compares the two strings s1 and s2.                                                                               
        It returns an integer less than, equal to, or greater than zero if s1 is found,                                                           
        respectively, to be less than, to match, or be greater than s2.                                                                           
        The strncmp() function is similar, except it only compares the first (at most) n characters of s1 and s2.                                 
 * RETURN VALUE                                                                                                                                   
        The strcmp() and strncmp() functions return an integer less than, equal to,                                                               
        or greater than zero if s1 (or the first n bytes thereof) is found, respectively,                                                         
        to  be less than, to match, or be greater than s2.                                                                                        
 */    
                               
  retval = strcmp( psrc, pdst);                                                                                                                 
    printf("[%s]-[%d]: retval = %d \n", __func__, __LINE__, retval);                                                                              
                                                                                                                                                  
    retval = strncmp( psrc, pdst, sSrcLen);                                                                                                       
    printf("[%s]-[%d]: retval = %d \n", __func__, __LINE__, retval);    

/* NAME                                                                                                                                           
        strcasecmp, strncasecmp - compare two strings ignoring case                                                                               
 * SYNOPSIS                                                                                                                                       
        #include <strings.h>                                                                                                                      
        int strcasecmp(const char *s1, const char *s2);
        int strncasecmp(const char *s1, const char *s2, size_t n);
 * DESCRIPTION
        The  strcasecmp()  function compares the two strings s1 and s2,
        ignoring the case of the characters.  // 忽略字母的大小写, (重点,与上面函数的区别)
        It returns an integer less than, equal to, or greater than zero
        if s1 is found, respectively, to be less than, to match, or be greater than s2.
        The strncasecmp() function is similar, except it only compares the first n characters of s1.
 *RETURN VALUE
        The strcasecmp() and strncasecmp() functions return an integer less than,
        equal to, or greater than zero if s1 (or the first n bytes thereof) is found,
        respectively,to be less than, to match, or be greater than s2.
 */


retval = strcasecmp( psrc, pdst);
    printf("[%s]-[%d]: retval = %d \n", __func__, __LINE__, retval);

    retval = strncasecmp( psrc, pdst, sSrcLen);
    printf("[%s]-[%d]: retval = %d \n", __func__, __LINE__, retval);

    return 0;
}

2 strstr, strcasestr

在源字串中寻找子字符串,一个对大小写敏感,前面函数对大小写不敏感

int main (void )
{
    
    const char *pSrc = "HelloworldComeon!";
//  const char *pDst = "heLLo";
//  const char *pDst = "WoRld";
    const char *pDst = "come";

/* NAME
        strstr, strcasestr - locate a substring                                                                                                   
 * SYNOPSIS
        #include <string.h>
        char *strstr(const char *haystack, const char *needle);                                                                                   
        #define _GNU_SOURCE                                                                                                                       
        #include <string.h>
        char *strcasestr(const char *haystack, const char *needle);
 * DESCRIPTION
        The strstr() function finds the first occurrence of the substring needle in the string haystack.  
        The terminating '\0' characters are not compared.
        The strcasestr() function is like strstr(), but ignores the case of both arguments.
 * RETURN VALUE
        These functions return a pointer to the beginning of the substring, or NULL if the substring is not found.
 */

    char *pRetval = strstr( (char *)pSrc, pDst);
    printf("[%s]-[%d] pRetval = %s \n", __func__, __LINE__, pRetval);
    /* 个人理解: 在源字符串中需找目标字符串,如果源字符串中存在,
                  则返回出现目标字符串的首地址,不存在,则返回空
                  对大小写字母敏感(区分大小写)*/    

    pRetval = strcasestr( (char *)pSrc, pDst );
    printf("[%s]-[%d] pRetval = %s \n", __func__, __LINE__, pRetval);

/* 个人理解: 在源字符串中需找目标字符串,如果源字符串中存在,
                  则返回出现目标字符串的首地址,不存在,则返回空
                  对大小写字母不敏感(忽略大小写)*/  

   
    return 0;
}

3 strchr strrchr,

在字符串中查找对应ASCII码为对应正数的字节,前面函数返回第一次查找到,后面函数返回最后一次查找到

int main (void )
{

/* NAME
            strchr, strrchr, -locate character in string
     SYNOPSIS
            #include <string.h>
            char *strchr(const char *s, int c);
            char *strrchr(const char *s, int c);
     DESCRIPTION
            The strchr() function returns a pointer to the first occurrence of the character c in the string s.
            The strrchr() function returns a pointer to the last occurrence of the character c in the string s.
     RETURN VALUE
            The strchr() and strrchr() functions return a pointer to the matched character or NULL if the character is not found.
     */
    const char *pSrc = "xxxxbooooobwwwwwwwbppppppbcccccccc";
    
    char *pRetval = strchr((char*)pSrc, 98);
    printf("[%s]-[%d] pRetval = %s \n", __func__, __LINE__, pRetval);


/* 个人理解:在目标字符串中,从字符串开头开始查找对已ASCII码为98的字节,
                 如果存在,则返回第一次查找到的字节地址,
                 不存在,则返回空*/
   pRetval = strrchr((char*)pSrc, 98);
    printf("[%s]-[%d] pRetval = %s \n", __func__, __LINE__, pRetval);


/* 个人理解:在目标字符串中,从字符串开头开始查找对已ASCII码为98的字节,
                 如果存在,则返回最后一次查找到的字节地址,
                 不存在,则返回空*/

    return 0;
}
~     
4 strcoll  比较字符串

int main (void )
{
    /* 
     * like the strcmp  
     */
    return 0;
} 
~                                                                                                                                                                                
~    

5 strdup  

类似与字符串拷贝函数,只是需要手动释放返回的字符串地址

int main (void )
{


/* NAME
            strdup, strndup - duplicate a string
     * SYNOPSIS
            #include <string.h>
            char *strdup(const char *s);
            char *strndup(const char *s, size_t n);
     * DESCRIPTION
            The strdup() function returns a pointer to a new string which is a duplicate of the string s.  
            Memory for the new string is obtained with malloc(3), and can be freed with free(3).
            The strndup() function is similar, but only copies at most n characters.  
            If s is longer than n, only n characters are copied, and a terminating null byte ('\0')  is added.
     * RETURN VALUE
            The strdup() function returns a pointer to the duplicated string, or NULL if insufficient memory was available.
     */


 const char *pSrc = "Hello, world!";
    
    char *pRetval = strdup( pSrc );
    printf("[%s]-[%d] pRetval = %s \n", __func__, __LINE__, pRetval);
    /* 复制目标字符串,返回字符串的内存地址需要自己手动释放 */  
    if( pRetval != NULL )
    {
        free( pRetval );
        pRetval = NULL;
    }
    
    pRetval = strndup( pSrc, 4);
    /* 复制指定长度目标字符串,返回字符串的内存地址需要自己手动释放 */  
    printf("[%s]-[%d] pRetval = %s \n", __func__, __LINE__, pRetval);
    if( pRetval != NULL )
    {
        free( pRetval );
        pRetval = NULL;
    }
    
    return 0;
}

6 strsep

分解字符串

int main (void )
{
/* NAME
        strsep - extract token from string
 * SYNOPSIS
        #include <string.h>
        char *strsep(char **stringp, const char *delim);
 * DESCRIPTION
        If  *stringp  is NULL, the strsep() function returns NULL and does nothing else.  
        Otherwise, this function finds the first token in the string *stringp, where tokens
        are delimited by symbols in the string delim.  This token is terminated with a '\0' character (by overwriting the delimiter)
        and *stringp is updated  to  point  past the token.  In case no delimiter was found,
        the token is taken to be the entire string *stringp, and *stringp is made NULL.
 * RETURN VALUE
        The strsep() function returns a pointer to the token, that is, it returns the original value of *stringp.
 * BUGS
        Be cautious when using this function.  If you do use it, note that:
        * This function modifies its first argument.
        * This function cannot be used on constant strings.
        * The identity of the delimiting character is lost.
 */

 char pSrc[] ="www.baidu.com.cn.education.mahuateng";
    char *pStringgp = pSrc;
    
    char *pRetval = NULL;
    
    
    while( (pRetval = strsep(&pStringgp, ".")) != NULL )    
  
 /* 个人理解:分解字符串为一组字符串, 此函数会改变字符串, 返回开始到 第一个出现 delim 的地方
                 并用 ‘\0’替换出现的第一个delim子字符串,如果没有 delim,则返回NULL,
                 此函数 比strtok好处是接受空字符串并且是线程安全的。
                 切割后源字符串变为第一个delim 到stringp 末尾,并过滤掉第一个delim。
                 如果stringp 为NULL,则返回NULL, */

 {   
        printf("[%s]-[%d] pStringgp = %s \n", __func__, __LINE__, pStringgp);
        printf("[%s]-[%d] pRetval = %s \n", __func__, __LINE__, pRetval);
    }


 /* 总结:strsep返回值为分割后的开始字符串,
             并将函数的第一个参数指针指向分割后的剩余字符串。
             它适用于分割关键字在两个字符串之间只严格出现一次的情况 */


 return 0;
}

7 strtok

通过分隔子字符串,分割目标字符串

int main (void )
{
/* NAME
        strtok - extract tokens from strings
 * SYNOPSIS
        #include <string.h>
        char *strtok(char *str, const char *delim);
 * DESCRIPTION
        The  strtok()  function parses a string into a sequence of tokens.  
        On the first call to strtok() the string to be parsed should be specified in str.  
        In each subsequent call that should parse the same string, str should be NULL.
 
        The delim argument specifies a set of characters that delimit the tokens in the parsed string.  
        The caller may specify different strings in delim in successive calls that parse the same string.
 
        Each  call  to  strtok() returns a pointer to a null-terminated string containing the next token.  
        This string does not include the delimiting character.  If no more tokens are found, strtok() returns NULL.
 
        A sequence of two or more contiguous delimiter characters in the parsed string is considered to be a single delimiter.  
        Delimiter characters at the start or  end  of the string are ignored.  Put another way: the tokens returned by strtok() are always non-empty strings.
 * RETURN VALUE
        The strtok() functions return a pointer to the next token, or NULL if there are no more tokens.
 */


   char pSrc[] = "www.baidu.com.education.mahuateng";
    char *pRetval = NULL;

    printf("[%s]-[%d] pSrc = %s\n", __func__, __LINE__, pSrc);
    pRetval = strtok( (char *)pSrc, ".");
  /* 个人理解:以"."子字符串为分隔符,分解目标字符串为一组子字符串,并且过滤掉分隔符
                 如果有子串分隔符的话,则将此处分隔符更改为'\0',目标字符串因此也发生改变,
                 当再次在以前的字符串中分解字串的话,则需要要把目标字串的参数置NULL,
                 不存在则返回NULL*/


while( NULL != pRetval )
    {
        printf("[%s]-[%d] pRetval = %s\n", __func__, __LINE__, pRetval);

        pRetval = strtok( NULL, ".");
    }

    printf("[%s]-[%d] pSrc = %s\n", __func__, __LINE__, pSrc);

 /* 总结:strtok内部记录上次调用字符串的位置,
             所以不支持多线程,可重入版本为strtok_r,有兴趣的可以研究一下。
             它适用于分割关键字在字符串之间是“单独”或是 “连续“在一起的情况。 */


  return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

max_min_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值