1

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.

4

2 回答 2

5

您不能修改现有的 System::String,它的内容是不可变的。StringBuilder但是您可以使用与 C# 相同的方式轻松构建一个新的:

String^ s = "abcd";
auto sb = gcnew System::Text::StringBuilder(s);
sb[2] = 'C';
s = sb->ToString();
于 2013-06-07T18:16:06.213 回答
0
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;

int main()
{
    string str="abcdefgh";

    str[2] = 'x';

    cout<<str<<endl;

    return 0;
}

您可以在此示例代码中找到答案。

于 2013-06-07T18:05:33.497 回答