C语言 如何从C中的字符串中删除回车符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3796479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:32:01  来源:igfitidea点击:

How to remove a carriage return from a string in C?

cstringcarriage-return

提问by syedmuhd

Possible Duplicate:
Remove characters from a string in C

可能的重复:
从 C 中的字符串中删除字符

Do you have an example of C code to remove carriage returns in a string?

您是否有删除字符串中回车符的 C 代码示例?

回答by paxdiablo

The simplest solution is to probably just process the string character by character, with pointers for source and destination:

最简单的解决方案可能只是逐个字符地处理字符串,并使用指向源和目标的指针:

#include <stdio.h>
#include <string.h>
int main (void) {
    char str[] = "This is a two-line\r\nstring with DOS line endings.\r\n";
    printf("%d [%s]\n",strlen(str), str);

    // ========================
    // Relevant code in this section.
    char *src, *dst;
    for (src = dst = str; *src != '
51 [This is a two-line
string with DOS line endings.
]
49 [This is a two-line
string with DOS line endings.
]
'; src++) { *dst = *src; if (*dst != '\r') dst++; } *dst = '
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void remove_char_from_string(char c, char *str)
{
    int i=0;
    int len = strlen(str)+1;

    for(i=0; i<len; i++)
    {
        if(str[i] == c)
        {
            // Move all the char following the char "c" by one to the left.
            strncpy(&str[i],&str[i+1],len-i);
        }
    }
}

void replace_char_from_string(char from, char to, char *str)
{
    int i = 0;
    int len = strlen(str)+1;

    for(i=0; i<len; i++)
    {
        if(str[i] == from)
        {
            str[i] = to;
        }
    }
}

int main(void) {
    char *original = "a\nmultiline\nstring";
    int original_len = strlen(original)+1;

    char *string = (char *)malloc(original_len);
    memset(string,0,original_len);
    strncpy(string,original,original_len);

    replace_char_from_string('\n',' ',string);
    printf("String: %s\n",string); // print: String: a multiline string

    remove_char_from_string(' ',string);
    printf("String: %s\n",string); // print: String: amultilinestring

    return 0;
}
'; // ======================== printf("%d [%s]\n",strlen(str), str); }

This sets up two pointers initially the same. It then copies a character and always advances the source pointer. However, it only advances the destination pointer if the character wasn't a carriage return. That way, all carriage returns are overwritten by either the next character or the null terminator. The output is:

这设置了两个最初相同的指针。然后它复制一个字符并始终推进源指针。但是,如果字符不是回车符,它只会推进目标指针。这样,所有回车都会被下一个字符或空终止符覆盖。输出是:

##代码##

This would be adequate for most strings. If your strings are truly massivein size (or this is something you need to do manytimes each second), you could look at a strchr-based solution since the library function will most likely be optimised.

这对于大多数字符串来说已经足够了。如果你的字符串是真正大规模的大小(或这是你需要做的事情很多次每秒),你可以看看在strchr基础的解决方案,因为库函数将最有可能得到优化。

回答by Pierre-Luc Simard

A carriage return or any other char is easy to replace in a string and even easier to replace. Your question does not really shows what you're looking for so I've included sample code for both removing and replacing a char in a string.

回车或任何其他字符很容易在字符串中替换,甚至更容易替换。您的问题并未真正显示您要查找的内容,因此我包含了用于删除和替换字符串中的字符的示例代码。

##代码##