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

发布时间:2023年12月17日

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|

文章来源:https://blog.csdn.net/Adrian503/article/details/134977276
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。