6

我最近开始在 C 中进行一个小玩具项目,并且一直在摸索模仿作为 python 字符串对象一部分的 strip() 功能的最佳方法。

阅读 fscanf 或 sscanf 表示字符串被处理到遇到的第一个空格。

fgets 也无济于事,因为我仍然有换行符。我确实尝试了 strchr() 来搜索空格并将返回的指针明确设置为 '\0' ,但这似乎不起作用。

4

4 回答 4

13

Python 字符串的strip方法会删除尾随和前导空格。在处理 C“字符串”(字符数组,\0 终止)时,问题的两半非常不同。

对于尾随空格:设置指向现有尾随 \0 的指针(或等效索引)。继续递减指针,直到它碰到字符串开头或任何非白色字符;在此终止向后扫描点之后将 \0 设置为右侧。

对于前导空格:将指针(或等效的索引)设置为字符串的开头;不断增加指针,直到它碰到一个非白色字符(可能是尾随 \0);memmove 字符串的其余部分,以便第一个非白色进入字符串的开头(对于后面的所有内容也是如此)。

于 2009-09-28T17:48:10.913 回答
12

strip() 或 trim() 函数没有标准的 C 实现。也就是说,这是 Linux 内核中包含的一个:

char *strstrip(char *s)
{
        size_t size;
        char *end;

        size = strlen(s);

        if (!size)
                return s;

        end = s + size - 1;
        while (end >= s && isspace(*end))
                end--;
        *(end + 1) = '\0';

        while (*s && isspace(*s))
                s++;

        return s;
}
于 2009-09-28T17:51:07.023 回答
1

如果您想删除一行的最后一个换行符,您可以使用以下代码段:

size_t s = strlen(buf);
if (s && (buf[s-1] == '\n')) buf[--s] = 0;

为了忠实地模仿 Python 的str.strip([chars])方法(我解释它的工作方式),您需要为新字符串分配空间,填充新字符串并返回它。之后,当您不再需要剥离的字符串时,您需要释放它曾经没有内存泄漏的内存。

或者您可以使用 C 指针并修改初始字符串并获得类似的结果。
假设您的初始字符串是"____forty two____\n"并且您想要去除所有下划线和 '\n'

____forty two___\n
^ ptr

如果您更改ptr为 'f' 并将第一个 '_' 替换为twoa之后'\0'的结果与 Python 的相同"____forty two____\n".strip("_\n");

____forty two\0___\n
    ^ptr

同样,这与 Python 不同。字符串已就地修改,没有第二个字符串,您无法恢复更改(原始字符串丢失)。

于 2009-09-28T18:07:47.813 回答
0

我编写了 C 代码来实现这个功能。我还写了一些简单的测试来确保我的函数能做一些有意义的事情。

此函数写入您提供的缓冲区,并且永远不应超过缓冲区的末尾,因此它不应该容易出现缓冲区溢出安全问题。

注意:只有Test()使用stdio.h,所以如果只需要函数,只需要包含ctype.h(对于isspace())和string.h(对于strlen())。

// strstrip.c -- implement white space stripping for a string in C
//
// This code is released into the public domain.
//
// You may use it for any purpose whatsoever, and you don't need to advertise
// where you got it, but you aren't allowed to sue me for giving you free
// code; all the risk of using this is yours.



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



// strstrip() -- strip leading and trailing white space from a string
//
// Copies from sIn to sOut, writing at most lenOut characters.
//
// Returns number of characters in returned string, or -1 on an error.
// If you get -1 back, then nothing was written to sOut at all.

int
strstrip(char *sOut, unsigned int lenOut, char const *sIn)
{
    char const *pStart, *pEnd;
    unsigned int len;
    char *pOut;

    // if there is no room for any output, or a null pointer, return error!
    if (0 == lenOut || !sIn || !sOut)
        return -1;

    pStart = sIn;
    pEnd = sIn + strlen(sIn) - 1;

    // skip any leading whitespace
    while (*pStart && isspace(*pStart))
        ++pStart;

    // skip any trailing whitespace
    while (pEnd >= sIn && isspace(*pEnd))
        --pEnd;

    pOut = sOut;
    len = 0;

    // copy into output buffer
    while (pStart <= pEnd && len < lenOut - 1)
    {
        *pOut++ = *pStart++;
        ++len;
    }


    // ensure output buffer is properly terminated
    *pOut = '\0';
    return len;
}


void
Test(const char *s)
{
    int len;
    char buf[1024];

    len = strstrip(buf, sizeof(buf), s);

    if (!s)
        s = "**null**";  // don't ask printf to print a null string
    if (-1 == len)
        *buf = '\0';  // don't ask printf to print garbage from buf

    printf("Input: \"%s\"  Result: \"%s\" (%d chars)\n", s, buf, len);
}


main()
{
    Test(NULL);
    Test("");
    Test(" ");
    Test("    ");
    Test("x");
    Test("  x");
    Test("  x   ");
    Test("  x y z   ");
    Test("x y z");
}
于 2009-09-28T21:13:15.663 回答