C 标准库 没有直接提供现成的 replace() 函数,需要手动实现查找子串位置(strstr()),拼接替换后的新字符串,手动操作字符串时 一定要注意内存管理,建议开大 buffer。如果是动态字符串,推荐使用 malloc() 分配新内存。

1、简单替换第一个出现的子串

使用 strstr 定位第一次出现的子串,用 strncpy strcat 拼接替换后的新字符串,避免直接在原字符串中操作,降低风险。

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

void str_replace_once(char *buffer, const char *oldWord, const char *newWord) {
    char temp[1024];
    char *pos = strstr(buffer, oldWord);
    if (pos == NULL) return;  // 没找到不替换

    // 复制 oldWord 之前的部分
    int len_before = pos - buffer;
    strncpy(temp, buffer, len_before);
    temp[len_before] = '\0';

    // 拼接 newWord
    strcat(temp, newWord);

    // 拼接 oldWord 之后的部分
    strcat(temp, pos + strlen(oldWord));

    // 把结果复制回原 buffer
    strcpy(buffer, temp);
}

int main() {
    char str[1024] = "I love programming in C";
    str_replace_once(str, "C", "C++");
    printf("%s\n", str);  // 输出:I love programming in C++
    return 0;
}

2、全部替换

使用 strstr 找位置,简单有效。处理多个匹配(循环查找)。替换后更新 buffer

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

// str_replace_all 函数
void str_replace_all(char *buffer, const char *oldWord, const char *newWord) {
    char temp[1024];
    char *pos;
    int oldLen = strlen(oldWord);
    int newLen = strlen(newWord);

    while ((pos = strstr(buffer, oldWord)) != NULL) {
        // 清空 temp
        temp[0] = '\0';
        // 复制 oldWord 之前的部分
        int len_before = pos - buffer;
        strncpy(temp, buffer, len_before);
        temp[len_before] = '\0';

        // 拼接 newWord
        strcat(temp, newWord);

        // 拼接 oldWord 之后的部分
        strcat(temp, pos + oldLen);

        // 更新 buffer
        strcpy(buffer, temp);
    }
}

int main() {
    char text[1024] = "Hello world! Welcome to the world of C programming.";
    const char *oldWord = "world";
    const char *newWord = "universe";

    printf("原字符串: %s\n", text);

    // 调用替换函数
    str_replace_all(text, oldWord, newWord);

    printf("替换后: %s\n", text);

    return 0;
}

3、使用 POSIX 正则库替换字符串中的内容

使用 正则表达式库(POSIX regex) 进行字符串替换,可以使用 库配合字符串处理来实现。

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

void regex_replace(const char *src, const char *pattern, const char *replacement, char *result, size_t result_size) {
    regex_t regex;
    regmatch_t pmatch[1];
    const char *cursor = src;
    char temp[1024];
    result[0] = '\0';  // 清空结果

    if (regcomp(®ex, pattern, REG_EXTENDED) != 0) {
        printf("正则编译失败\n");
        return;
    }

    while (regexec(®ex, cursor, 1, pmatch, 0) == 0) {
        // 拷贝匹配前的内容
        strncat(result, cursor, pmatch[0].rm_so);
        // 拼接替换内容
        strncat(result, replacement, result_size - strlen(result) - 1);
        // 移动 cursor
        cursor += pmatch[0].rm_eo;
    }
    // 拼接剩余未匹配的部分
    strncat(result, cursor, result_size - strlen(result) - 1);

    regfree(®ex);
}

int main() {
    const char *text = "hello 123 world 456";
    const char *pattern = "[0-9]+";   // 匹配数字
    const char *replacement = "***";  // 替换为 ***
    char result[1024];

    regex_replace(text, pattern, replacement, result, sizeof(result));

    printf("原文: %s\n", text);
    printf("替换后: %s\n", result);

    return 0;
}

推荐文档