ansiString问题

来源:百度知道 编辑:UC知道 时间:2024/06/19 03:19:58
AnsiString str = "ab,cd,ef";
要把,号换成"/"怎么做
就是把指定位置的字符重新赋值该怎么做?

利用一个指针p先指向字符串的首地址,然后用判断
while (*p != '\0')
{
if (',' == (*p))
{
*p = '/';
}
p++;
}

#include <iostream>
#include <string>

using namespace std;

int main()
{
string str("a,b,c,d");
size_t pos;
while ((pos = str.find(",")) != string::npos )
str.replace(pos, 1, "/");
cout << str << endl;
system("pause");
return 0;
}

既然是用string,当然要用其自带的函数方便啊!

好像有个replace函数

//方法一:利用string
#include <iostream> 
#include <string> 

using namespace std; 

int main() 

    string str("a,b,c,d"); 
    size_t pos; 
    while ((pos