C/C++字符串处理函数

简介: C/C++字符串处理函数

C/C++中常用字符串处理函数包括:

strlen:用于求字符串长度,函数原型为:size_t strlen(const char *s);

char str[] = "hello world";
int len = strlen(str); // len = 11
c

strcpy:用于复制字符串,函数原型为:char *strcpy(char *dest, const char *src);

char str1[] = "hello world";
char str2[100];
strcpy(str2, str1);
c

strcmp:用于比较字符串大小,函数原型为:int strcmp(const char *s1, const char *s2);

char str1[] = "hello";
char str2[] = "world";
int result = strcmp(str1, str2); // result = -15
c

strcat:用于拼接字符串,函数原型为:char *strcat(char *dest, const char *src);

char str1[100] = "hello";
char str2[] = "world";
strcat(str1, str2); // str1 = "helloworld"
c

strtok:用于分割字符串,函数原型为:char *strtok(char *str, const char *delim);

char str[] = "hello|world|";
char *ptr = strtok(str, "|");
while(ptr != NULL) {
printf("%s\n", ptr); // 输出 hello、world
ptr = strtok(NULL, "|");
}
c

除此之外还有很多其他常见的字符串处理函数,在实际开发中需要根据需要灵活应用。

相关文章
|
7天前
|
存储 算法 搜索推荐
【C++】类的默认成员函数
【C++】类的默认成员函数
|
28天前
|
C++ 运维
开发与运维函数问题之析构函数在C++类中起什么作用如何解决
开发与运维函数问题之析构函数在C++类中起什么作用如何解决
29 11
|
28天前
|
C++ 运维
开发与运维函数问题之C++类的简单示例如何解决
开发与运维函数问题之C++类的简单示例如何解决
45 10
|
28天前
|
存储 C++ 运维
开发与运维函数问题之使用C++标准库中的std::function来简化回调函数的使用如何解决
开发与运维函数问题之使用C++标准库中的std::function来简化回调函数的使用如何解决
31 6
|
28天前
|
编译器 C++ 运维
开发与运维函数问题之函数的返回类型如何解决
开发与运维函数问题之函数的返回类型如何解决
25 6
|
5天前
|
Dart 编译器 API
Dart ffi 使用问题之在C++线程中无法直接调用Dart函数的问题如何解决
Dart ffi 使用问题之在C++线程中无法直接调用Dart函数的问题如何解决
|
1月前
|
C++
C++ string中的函数和常用用法
C++ 中string中的函数和常用用法
22 4
|
11天前
|
JavaScript C++
【C++ visual studio】解决VS报错:error C2447: “{”: 缺少函数标题(是否是老式的形式表?)【亲测有效,无效捶我】
【C++ visual studio】解决VS报错:error C2447: “{”: 缺少函数标题(是否是老式的形式表?)【亲测有效,无效捶我】
|
1月前
|
存储 C++
【C++】string类的使用③(非成员函数重载Non-member function overloads)
这篇文章探讨了C++中`std::string`的`replace`和`swap`函数以及非成员函数重载。`replace`提供了多种方式替换字符串中的部分内容,包括使用字符串、子串、字符、字符数组和填充字符。`swap`函数用于交换两个`string`对象的内容,成员函数版本效率更高。非成员函数重载包括`operator+`实现字符串连接,关系运算符(如`==`, `<`等)用于比较字符串,以及`swap`非成员函数。此外,还介绍了`getline`函数,用于按指定分隔符从输入流中读取字符串。文章强调了非成员函数在特定情况下的作用,并给出了多个示例代码。