string help

hello lets say we have string x = "abcdefgh";
what i want to do is to remove letters for example i would like to make from this is just bcdefgh and then cdefgh i dont want to use substring and i wanted to do smth like x[0] = ""; so i can clear that space in string but it doesnot work , any ideas guys ? thanks.
http://www.cplusplus.com/reference/string/string/replace/

Edit : Example -

1
2
3
4
5
string x = "Tarik Neaj";
	
x.replace(0,2, ""); // First parameter tells it where to begin. Second how many characters to replace, third what to replace it with.

cout << x << endl;


output:

rik Neaj


Last edited on
thanks tarik so much :)
My pleasure :)
string z = "12345";

for (int i = 1; i < z.length(); i++)
{
z.replace(0, i, "");
cout << z << endl;
}


output should be like 2345 345 45 5 but it isnot i didnot get the pointi dont understand that >D
// got it i know where was mistake.
Last edited on
Hey, please use code tags for all of your code - http://www.cplusplus.com/articles/jEywvCM9/

Problem is that. Its replacing them completely, thats what this function does.

you have your string to begin with

string z = "12345".

Your loop runs 4 times. First loop you replace 1 with nothing. So it outputs
2345
.

Now your string looks like this - "2345".

So the second round of the loop, you replace the first position string, which is now 2, and then 2 characters. So it removed 2 and 3, now your output is
45
.

If you want the output you requested, you have to re-give your string the original value.

1
2
3
4
5
6
7
8
string z = "12345";

	for (int i = 1; i < z.length(); i++)
	{
		z.replace(0, i, "");
		cout << z << endl;
		z = "12345"; // here
	}
Last edited on
thanks , i already created other "algorithm" to do that using replace func , thanks anyway
Replace is unnecessarily overkill.
Use erase (check out the example code there):
http://www.cplusplus.com/reference/string/string/erase/
Yeh in this case erase should be used over replace, I just showed him replace incase he needed to not just replace stuff with a whit space.
thanks SGH i already used replace and it works as i wanted to so its ok , thanks :)
Topic archived. No new replies allowed.