Reputation: 3553
i have
cost char* a="test_r.txt"
i want to strip the _r and add _n instead of it so that it becomes "test_n.txt" and save it in const char* b;
what is the easiest way to do it ?
Upvotes: 0
Views: 636
Reputation: 199
Like mentioned before, if you put your const char *
into a std::string
, you can change your characters. Regarding the replacing of a constant that is longer than one character you can use the find
method of std::string
:
const char *a = "test_r_or_anything_without_r.txt";
std::string a_copy(a);
std::string searching("_r");
std::string replacing("_n");
size_t pos_r = 0;
while (std::string::npos != (pos_r = a_copy.find(searching, pos_r)))
{
a_copy.replace(a_copy.begin() + pos_r,
a_copy.begin() + pos_r + searching.length(),
replacing.begin(), replacing.end());
}
const char *b = a_copy.c_str();
Upvotes: 1
Reputation: 258568
You can't directly modify the contents of a
, so you'll need some copying:
std::string aux = a;
aux[5] = 'n';
const char* b = aux.c_str();
Upvotes: 5