c++ string 结束符‘00’

昨天输出string类型时总是出错,发现输出到文件的一行里多了^@,输出到console却看不到,debug发现,string类型中多了00,这主要由于我想要用截掉字符串最后一位,所以把字符串的最后一位换成了,却发生了错误。

例如:

              string tmp = “Hello;”

              string tmp2 = "World"

我想得到:

              tmp = “HelloWorld”

首先用了:

              tmp[tmp.length()-1] = '';   (1)

              tmp += tmp2

最后输出的却是:

              “Hello00World”

原因是什么还不了解,只是大概知道string类型不是以为结束符的。

解决方法,将(1)处改为:

              tmp  = tmp.substr(0,tmp.length()-1);

原文地址:https://www.cnblogs.com/dorothychai/p/3463657.html