C ++中的字符串中的NUL char [英] NUL char in strings in C++

查看:96
本文介绍了C ++中的字符串中的NUL char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我试图用'\0'替换第一个字符。

In the below code I am trying to replace the first character with '\0'.

我希望它能打印一个空字符串,但在输出中它会忽略该字符串并显示其余字符。

I expected it to print an empty string, but in the output it just omits that and displays the rest of the characters.

int main()
{
    string str;
    cin >> str;

    str[0] = '\0';

    cout << str << "\n";
    return 0;
}

输出:

   testing
   esting

如何在C ++中终止字符串?

How do I terminate the string in C++?

PS:我正在尝试这种在其他问题之间终止的方法,该问题需要在两者之间终止。

PS: I was trying this approach of terminating the string in a different question in which I need to terminate in between.

推荐答案

std :: string 不是以null结尾的字符串。如果要清空它,请使用 clear()方法。如果要删除字符串的元素,请使用 erase()方法。

std::string is not a null terminated string. If you want to empty it use the clear() method. If you want to remove an element of the string use the erase() method.

这篇关于C ++中的字符串中的NUL char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆