|
|
# strstr, strpbrk, strchr 函数详解
这三个都是C语言中的字符串查找函数,但功能有所不同:
## 1. strstr - 查找子字符串
### 函数原型
- char *strstr(const char *haystack, const char *needle);
复制代码
### 功能
在 `haystack` 字符串中查找 `needle` 子字符串的第一次出现位置。
### 示例
- #include <stdio.h>
- #include <string.h>
- int main() {
- char text[] = "Hello, welcome to FishC!";
- char *result;
-
- result = strstr(text, "welcome");
- if (result != NULL) {
- printf("找到子串: %s\n", result); // 输出: welcome to FishC!
- printf("位置: %ld\n", result - text); // 输出: 7
- } else {
- printf("未找到子串\n");
- }
-
- return 0;
- }
复制代码
## 2. strpbrk - 查找字符集合中的任意字符
### 函数原型
- char *strpbrk(const char *str, const char *accept);
复制代码
### 功能
在 `str` 字符串中查找 `accept` 字符集合中任意字符的第一次出现位置。
### 示例
- #include <stdio.h>
- #include <string.h>
- int main() {
- char text[] = "Hello, world! 123";
- char *result;
-
- // 查找数字字符
- result = strpbrk(text, "0123456789");
- if (result != NULL) {
- printf("找到数字: %s\n", result); // 输出: 123
- printf("位置: %ld\n", result - text); // 输出: 14
- }
-
- // 查找标点符号
- result = strpbrk(text, ",.!");
- if (result != NULL) {
- printf("找到标点: %s\n", result); // 输出: , world! 123
- }
-
- return 0;
- }
复制代码
## 3. strchr - 查找特定字符
### 函数原型
- char *strchr(const char *str, int c);
复制代码
### 功能
在 `str` 字符串中查找字符 `c` 的第一次出现位置。
### 示例
- #include <stdio.h>
- #include <string.h>
- int main() {
- char text[] = "Hello, world!";
- char *result;
-
- // 查找逗号
- result = strchr(text, ',');
- if (result != NULL) {
- printf("找到逗号: %s\n", result); // 输出: , world!
- printf("位置: %ld\n", result - text); // 输出: 5
- }
-
- // 查找字符 'o'
- result = strchr(text, 'o');
- if (result != NULL) {
- printf("第一个 'o': %s\n", result); // 输出: o, world!
- }
-
- return 0;
- }
复制代码
## 综合对比示例
- #include <stdio.h>
- #include <string.h>
- int main() {
- char text[] = "The quick brown fox jumps over the lazy dog";
-
- printf("原始字符串: %s\n\n", text);
-
- // 使用 strstr 查找单词
- char *substr = strstr(text, "brown");
- printf("strstr("brown"): %s\n", substr ? substr : "未找到");
-
- // 使用 strpbrk 查找元音字母
- char *vowel = strpbrk(text, "aeiouAEIOU");
- printf("strpbrk(元音): %s\n", vowel ? vowel : "未找到");
-
- // 使用 strchr 查找空格
- char *space = strchr(text, ' ');
- printf("strchr(' '): %s\n", space ? space : "未找到");
-
- return 0;
- }
复制代码
## 实际应用场景
### 1. 解析文件路径
- #include <stdio.h>
- #include <string.h>
- int main() {
- char path[] = "/home/user/documents/file.txt";
-
- // 提取文件名
- char *filename = strrchr(path, '/'); // 从右边查找
- if (filename != NULL) {
- printf("文件名: %s\n", filename + 1); // 跳过 '/'
- }
-
- // 提取文件扩展名
- char *extension = strrchr(path, '.');
- if (extension != NULL) {
- printf("扩展名: %s\n", extension + 1); // 跳过 '.'
- }
-
- return 0;
- }
复制代码
### 2. 验证字符串格式
- #include <stdio.h>
- #include <string.h>
- #include <stdbool.h>
- bool is_valid_email(const char *email) {
- // 简单邮箱格式验证
- return (strchr(email, '@') != NULL) &&
- (strchr(email, '.') != NULL);
- }
- int main() {
- char email1[] = "user@example.com";
- char email2[] = "invalid-email";
-
- printf("%s: %s\n", email1, is_valid_email(email1) ? "有效" : "无效");
- printf("%s: %s\n", email2, is_valid_email(email2) ? "有效" : "无效");
-
- return 0;
- }
复制代码
## 总结
| 函数 | 功能 | 返回值 |
|------|------|--------|
| `strstr` | 查找子字符串 | 子字符串首次出现的位置 |
| `strpbrk` | 查找字符集合中的任意字符 | 匹配字符首次出现的位置 |
| `strchr` | 查找特定字符 | 字符首次出现的位置 |
**共同特点:**
- 都返回指向找到位置的指针
- 如果没找到,返回 `NULL`
- 都是区分大小写的
- 都在 `string.h` 头文件中声明
这些函数在处理字符串搜索和解析时非常有用,可以根据具体需求选择合适的函数。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|