I have a String^ variable. It's a string, I would like to replace a character at ith position in it. I have seen C# examples where they use StringBuilder. What will work for C++ ?
I am using VS 2012 on Windows 7.
I have a String^ variable. It's a string, I would like to replace a character at ith position in it. I have seen C# examples where they use StringBuilder. What will work for C++ ?
I am using VS 2012 on Windows 7.
您不能修改现有的 System::String,它的内容是不可变的。StringBuilder
但是您可以使用与 C# 相同的方式轻松构建一个新的:
String^ s = "abcd";
auto sb = gcnew System::Text::StringBuilder(s);
sb[2] = 'C';
s = sb->ToString();
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main()
{
string str="abcdefgh";
str[2] = 'x';
cout<<str<<endl;
return 0;
}
您可以在此示例代码中找到答案。