C++ 字符串操作常见函数
1 //字符串拷贝,排除指定字符 2 char *strcpy_exclude_char(char *dst, const int dst_len, const char *src, const char *exclude_list) 3 { 4 int i = 0, j = 0, flag = 0; 5 const char *p = NULL; 6 if (dst == NULL && src == NULL)return NULL; 7 if (dst == src)return dst; 8 for (; j < dst_len && src[i] != '\0'; ++i) 9 { 10 flag = 0; 11 p = exclude_list; 12 while (p && *p != '\0') 13 { 14 if (*p == src[i]){ flag = 1; break; } 15 p++; 16 } 17 if (flag == 0)dst[j++] = src[i]; 18 } 19 dst[j] = '\0'; 20 return dst; 21 }
1 //字符串拷贝,替换指定字符 2 char *strcpy_replace_char(char *dst, const int dst_len, const char *src, char aim, char rep) 3 { 4 int i = 0; 5 if (dst == NULL && src == NULL)return NULL; 6 if (dst == src)return dst; 7 for (; i < dst_len && src[i] != '\0'; ++i) 8 { 9 if (src[i] == aim) 10 { 11 dst[i] = rep; 12 } 13 else 14 { 15 dst[i] = src[i]; 16 } 17 } 18 dst[i] = '\0'; 19 return dst; 20 }
//字符串原址替换指定字符 char * str_replace_char(char *dst, char aim, char rep) { int i = 0; if (dst == NULL)return NULL; for (; dst[i] != '\0';++i) { if (dst[i] == aim) { dst[i] = rep; } } return dst; }