C语言常用字符串函数总结

1、将字符串转换为数字

strtol    根据进制转化为 long int型数字,比如要将字符串"1a"转化成16进制数字 0x1a

strtoul  根据进制转化为 unsigned long int 型数字。比如要将字符串"1a"转化成16进制数字 0x1a

atoi  将字符串转化为int型数字。比如要将字符串"123"转化成10进制数字123

/* strtoul */

    const char* str = "1a";   
    char* endptr = NULL;   
    unsigned long int n;   
    n = strtoul(str, &endptr, 16);   
    printf("The number(unsigned long int) is: %lu\n", n);   


/* strtol */

    const char* str = "1a";   
    char* endptr = NULL;   
    unsigned long int n;   
    n = strtol(str, &endptr, 16);   
    printf("The number(long int) is: %l\n", n);   



/* atoi */

    const char* str = "123";     
    unsigned long int n;   
    n = atoi(str);   
    printf("The number(int) is: %d\n", n);   

2、在字符串中查找字符串/字符

char *strstr(const char *haystack, const char *needle)

函数功能:在字符串haystack中查找字符串needle,如果存在则返回第一次出现的字串的首地址,如果不存在返回NULL。

include <stdio.h>
#include <string.h>


int main () {
   const char haystack[20] = "TutorialsPoint";
   const char needle[10] = "Point";
   char *ret;

   ret = strstr(haystack, needle);

   printf("The substring is: %s\n", ret);
   
   return(0);
}

# result
The substring is: Point

参考:C library function - strstr()

char *strchr(const char *str, int c)

函数功能:在字符串str中查找字符c,如果存在则返回第一次出现的首地址,如果不存在返回NULL。

#include <stdio.h>
#include <string.h>

int main () {
   const char str[] = "https://www.tutorialspoint.com";
   const char ch = '.';
   char *ret;

   ret = strchr(str, ch);

   printf("String after |%c| is - |%s|\n", ch, ret);
   
   return(0);
}

# result
String after |.| is - |.tutorialspoint.com|

参考:C library function - strchr()

3、根据分隔符,将字符串分割成一组字串

比如这种字符串  "20 10 0f"    "1a:2f:5c:26:34:56" ,如果分割后转化成数字,一般会搭配strtoul  使用。

char *strtok(char *str, const char *delim)
  • str − 要被分割的字符串,再次调用要把str设为NULL,If str is NULL, the saved pointer in SAVE_PTR is used as the next starting point.

  • delim − This is the C string containing the delimiters. These may vary from one call to another.

 Breaks string str into a series of tokens using the delimiter delim. 返回第一个被找到的token的首地址。如果后面没有了,则返回NULL。

#include <string.h>
#include <stdio.h>

int main () {
   char str[80] = "This is - www.tutorialspoint.com - website";
   const char s[2] = "-";
   char *token;
   
   /* get the first token */
   token = strtok(str, s);
   
   /* walk through other tokens */
   while( token != NULL ) {
      printf( " %s\n", token );
    
      token = strtok(NULL, s);
   }
   
   return(0);
}

#result

This is 
  www.tutorialspoint.com 
  website

参考:C library function - strtok()

4、字符串连接

char *strcat(char *dest, const char *src)

Appends the string pointed to by src to the end of the string pointed to by dest

This function returns a pointer to the resulting string dest.

  • dest − This is pointer to the destination array, which should contain a C string, and should be large enough to contain the concatenated resulting string.

  • src − This is the string to be appended. This should not overlap the destination.

#include <stdio.h>
#include <string.h>

int main () {
   char src[50], dest[50];

   strcpy(src,  "This is source");
   strcpy(dest, "This is destination");

   strcat(dest, src);

   printf("Final destination string : |%s|", dest);
   
   return(0);
}
char *strncat(char *dest, const char *src, size_t n)

将src前n个字节 append到dest
  • dest − This is pointer to the destination array, which should contain a C string, and should be large enough to contain the concatenated resulting string which includes the additional null-character.

  • src − This is the string to be appended.

  • n − This is the maximum number of characters to be appended.

#include <stdio.h>
#include <string.h>

int main () {
   char src[50], dest[50];

   strcpy(src,  "This is source");
   strcpy(dest, "This is destination");

   strncat(dest, src, 10);

   printf("Final destination string : |%s|", dest);
   
   return(0);
}

#result

Final destination string : |This is destinationThis is so|

  • 21
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值