清空 C 字符串的正确方法

发布于 2024-12-14 21:29:39 字数 185 浏览 3 评论 0原文

我一直在做一个 C 项目,需要我经常处理字符串。通常,我用 C++ 进行编程,所以这与仅仅说 string.empty() 有点不同。

我想知道在 C 中清空字符串的正确方法是什么。会是这样吗?

buffer[80] = "Hello World!\n";

// ...

strcpy(buffer, "");

I've been working on a project in C that requires me to mess around with strings a lot. Normally, I do program in C++, so this is a bit different than just saying string.empty().

I'm wondering what would be the proper way to empty a string in C. Would this be it?

buffer[80] = "Hello World!\n";

// ...

strcpy(buffer, "");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

难理解 2024-12-21 21:29:39

这取决于你所说的“空”是什么意思。如果您只想要一个零长度的字符串,那么您的示例就可以工作。

这也将起作用:

buffer[0] = '\0';

如果您想将字符串的整个内容归零,可以这样做:

memset(buffer,0,strlen(buffer));

但这仅适用于将第一个 NULL 字符归零。

如果字符串是静态数组,则可以使用:

memset(buffer,0,sizeof(buffer));

It depends on what you mean by "empty". If you just want a zero-length string, then your example will work.

This will also work:

buffer[0] = '\0';

If you want to zero the entire contents of the string, you can do it this way:

memset(buffer,0,strlen(buffer));

but this will only work for zeroing up to the first NULL character.

If the string is a static array, you can use:

memset(buffer,0,sizeof(buffer));
孤檠 2024-12-21 21:29:39

另外两种方法是 strcpy(str, "");string[0] = 0

真正删除变量内容(以防您有无法正常工作的脏代码正确地使用上面的片段:P)使用如下例所示的循环。

#include <string.h>

...

int i=0;
for(i=0;i<strlen(string);i++)
{
    string[i] = 0;
}

如果您想从头开始清除动态分配的字符数组,
你可以使用 malloc() 和 memset() 的组合,或者 - 这更快 - calloc() ,它与 malloc 做同样的事情,但用 Null 初始化整个数组。

最后我希望你记住你的运行时间。
更重要的是,如果您正在处理巨大的数组(6 位及以上),您应该尝试将第一个值设置为 Null,而不是通过整个字符串运行 memset()。

一开始它可能看起来更脏,但速度更快。您只需要更加关注您的代码;)

我希望这对任何人都有用;)

Two other ways are strcpy(str, ""); and string[0] = 0

To really delete the Variable contents (in case you have dirty code which is not working properly with the snippets above :P ) use a loop like in the example below.

#include <string.h>

...

int i=0;
for(i=0;i<strlen(string);i++)
{
    string[i] = 0;
}

In case you want to clear a dynamic allocated array of chars from the beginning,
you may either use a combination of malloc() and memset() or - and this is way faster - calloc() which does the same thing as malloc but initializing the whole array with Null.

At last i want you to have your runtime in mind.
All the way more, if you're handling huge arrays (6 digits and above) you should try to set the first value to Null instead of running memset() through the whole String.

It may look dirtier at first, but is way faster. You just need to pay more attention on your code ;)

I hope this was useful for anybody ;)

月竹挽风 2024-12-21 21:29:39

取决于你所说的清空是什么意思。如果您只想要一个空字符串,您可以这样做

buffer[0] = 0;

如果您想将每个元素设置为零,请执行

memset(buffer, 0, 80);

Depends on what you mean by emptying. If you just want an empty string, you could do

buffer[0] = 0;

If you want to set every element to zero, do

memset(buffer, 0, 80);
逆夏时光 2024-12-21 21:29:39

如果您试图清除接收字符串的接收缓冲区,我发现最好的方法是使用如上所述的 memset。原因是,无论下一个接收到的字符串有多大(当然限于 sizeof buffer),如果写入已预先清零的缓冲区,它将自动成为 asciiz 字符串。

If you are trying to clear out a receive buffer for something that receives strings I have found the best way is to use memset as described above. The reason is that no matter how big the next received string is (limited to sizeof buffer of course), it will automatically be an asciiz string if written into a buffer that has been pre-zeroed.

我不在是我 2024-12-21 21:29:39

我是初学者,但是...据我所知,最好的方法是

strncpy(dest_string,"",strlen(dest_string));

I'm a beginner but...Up to my knowledge,the best way is

strncpy(dest_string,"",strlen(dest_string));
梦亿 2024-12-21 21:29:39

对于那些点击进入此问题以查看有关 bzeroexplicit_bzero 的一些信息的人:

  • bzero:此函数已弃用
  • explicit_bzero:这不是标准 c 函数,并且在某些BSD 的

进一步解释:

For those who clicked into this question to see some information about bzero and explicit_bzero:

  • bzero: this function is deprecated
  • explicit_bzero: this is not a standard c function and is absent on some of the BSDs

Further explanation:

轻许诺言 2024-12-21 21:29:39

需要字符串的名称及其长度
将所有字符归零
其他方法可能会在遇到的第一个零处停止

    void strClear(char p[],u8 len){u8 i=0; 
        if(len){while(i<len){p[i]=0;i++;}}
    }

needs name of string and its length
will zero all characters
other methods might stop at the first zero they encounter

    void strClear(char p[],u8 len){u8 i=0; 
        if(len){while(i<len){p[i]=0;i++;}}
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文