如何从C中的给定字符串中删除\ n或\ t?

如何在C中删除所有\ n和\ t的字符串?

这适用于我快速而肮脏的测试。 是否到位:

#include  void strip(char *s) { char *p2 = s; while(*s != '\0') { if(*s != '\t' && *s != '\n') { *p2++ = *s++; } else { ++s; } } *p2 = '\0'; } int main() { char buf[] = "this\t is\na\t test\n test"; strip(buf); printf("%s\n", buf); } 

为了安抚Chris,这里有一个版本,它将首先复制并返回它(因此它将在文字上工作)。 您需要free结果。

 char *strip_copy(const char *s) { char *p = malloc(strlen(s) + 1); if(p) { char *p2 = p; while(*s != '\0') { if(*s != '\t' && *s != '\n') { *p2++ = *s++; } else { ++s; } } *p2 = '\0'; } return p; } 

如果要将\ n或\ t替换为其他内容 ,可以使用函数strstr()。 它返回一个指针,指向具有特定字符串的函数中的第一个位置。 例如:

 // Find the first "\n". char new_char = 't'; char* pFirstN = strstr(szMyString, "\n"); *pFirstN = new_char; 

您可以在循环中运行它以查找所有\ n和\ t。

如果你想“剥离”它们,即从字符串中删除它们 ,你需要实际使用与上面相同的方法,但每次找到\ n或\ t时,都要复制字符串“back”的内容,所以“这个测试”变成:“这是一个测试”。

你可以用memmove(不是memcpy,因为src和dst指向重叠的内存)来做到这一点,如下所示:

 char* temp = strstr(str, "\t"); // Remove \n. while ((temp = strstr(str, "\n")) != NULL) { // Len is the length of the string, from the ampersand \n, including the \n. int len = strlen(str); memmove(temp, temp + 1, len); } 

您需要再次重复此循环以删除\ t。

注意:这两种方法都可以就地工作。 这可能不安全! (阅读Evan Teran对细节的评论 。此外,这些方法效率不高,尽管它们对某些代码使用库函数而不是自己编写代码。

基本上,您有两种方法可以执行此操作:您可以创建原始字符串的副本,减去所有'\t''\n'字符,或者您可以“就地”删除字符串。 但是,我敢打赌,第一种选择会更快,我保证会更安全。

所以我们将创建一个函数:

 char *strip(const char *str, const char *d); 

我们想使用strlen()malloc()来分配一个与str缓冲区大小相同的新char *缓冲区。 然后我们str进行。 如果d 没有包含该字符,我们将其复制到新缓冲区中。 我们可以使用像strchr()这样的东西来查看每个字符是否在字符串d 。 一旦我们完成,我们有一个新的缓冲区,我们的旧缓冲区的内容减去字符串d中的字符,所以我们只返回它。 我不会给你示例代码,因为这可能是家庭作业,但这里是示例用法,向您展示它如何解决您的问题:

 char *string = "some\n text\t to strip"; char *stripped = strip(string, "\t\n"); 

这是一个ac字符串函数,它将在accept找到任何字符并返回指向该位置的指针,如果找不到则返回NULL。

 #include  char *strpbrk(const char *s, const char *accept); 

例:

 char search[] = "a string with \t and \n"; char *first_occ = strpbrk( search, "\t\n" ); 

first_occ将指向搜索中的\ t或15个字符。 您可以替换然后再次呼叫循环直到所有已被替换。

我喜欢让标准库尽可能多地完成工作,所以我会使用类似于Evan的解决方案但使用strspn()strcspn()

 #include  #include  #include  #define SPACE " \t\r\n" static void strip(char *s); static char *strip_copy(char const *s); int main(int ac, char **av) { char s[] = "this\t is\na\t test\n test"; char *s1 = strip_copy(s); strip(s); printf("%s\n%s\n", s, s1); return 0; } static void strip(char *s) { char *p = s; int n; while (*s) { n = strcspn(s, SPACE); strncpy(p, s, n); p += n; s += n + strspn(s+n, SPACE); } *p = 0; } static char *strip_copy(char const *s) { char *buf = malloc(1 + strlen(s)); if (buf) { char *p = buf; char const *q; int n; for (q = s; *q; q += n + strspn(q+n, SPACE)) { n = strcspn(q, SPACE); strncpy(p, q, n); p += n; } *p++ = '\0'; buf = realloc(buf, p - buf); } return buf; }