Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, i want know how can i delete characters of an string, for example, delete the characters 0 and 4 of this string.
string frase  = "Ayer estaba solo";

Best regards
Ángel Manuel
Posted

To delete 0 try something like this -

while(frase.indexof(0)!=-1)
{
frase.Remove(frase.indexOf(0));
}


You can paramterize this method to pass all values.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Feb-11 1:40am    
Sorry Abhinav, this will not work. String is immutable. When you call string method, you return another string but discard the return. Test it -- in won't work.
Also, this is very ineffective. Please fix.
--SA
Abhinav S 3-Feb-11 1:45am    
The OP has changed the tag from C# to C++. So this answer does not hold good anyway.
Sergey Alexandrovich Kryukov 3-Feb-11 1:50am    
Abhinav, are you sure? I also saw C#, but where is the past revision of the Question? Is is possible?
--SA
Sergey Alexandrovich Kryukov 3-Feb-11 1:44am    
My answer is using StringBuilder which is designed for string manipulations and optimized for speed. Please see.
--SA
Sergey Alexandrovich Kryukov 3-Feb-11 1:46am    
Thank you.
--SA
Here is how:

C#
string frase  = "Ayer estaba solo";
System.Text.StringBuilder sb = new System.Text.StringBuilder(frase);
sb.Remove(1, 4);
frase = sb.ToString();


It's important to use StringBuilder for speed, even simple concatenations are faster then with string. The method string.Format is also faster then string equivalent concatenations.

Attention! OP changed a tag from C# to C++. I cannot be hold responsible for that. My answer was in C# when the tag was C#. Please do not down-vote based on that.



—SA
 
Share this answer
 
v3
Use erase function : Removes an element or a range of elements in a string from a specified position.

string frase = "Ayer estaba solo";
frase.erase(0,1);
First param is
_Pos: The index of the first character in the string to be removed.
_Count: The number of elements that will be removed if there are as many in the range of the string beginning with _Pos.
 
Share this answer
 
I would try a method similar to this.
Note: i just wrote this method on the fly to give the OP another idea.

C#
string remove_char_range(string const& str, unsigned const& start, unsigned const& end)
{
string newStr = "";
unsigned  i = 0;
for_each(str.cbegin(),str.cend(),[&i,&newStr, &start, &end](char const& ch)
{
//if i is in the range provided do not doing anything. else add to the new string
if (i >= start && i <= end)
{
}
else
newStr += ch;
//increment i
i++;
});
return newStr;
}



I just thought I would give it a try. if anyone has a comment i would like to hear it. I am just a student and I am sure there is a better way to do this.
robNO.
 
Share this answer
 
Comments
Niklas L 4-Feb-11 4:10am    
This is rather inefficient. The function also has an awkward signature for mimicking stl. What is wrong with string.erase()? It does what you try in a much more efficient manner. I don't see the point in giving the OP another idea, when there already is an answer both more efficient and robust. Since you're a student, read up on string.capacity().
RobNO 4-Feb-11 18:24pm    
Thanks for your reply!!! How can I make this method more efficient? What is awkward about the way i wrote the signature, It is very descriptive is it not(maybe to descriptive)? Nothing is wrong with string.erase(), however being a student I like to try and create my own methods in order to improve. You don't see the point in giving the original poster an idea to perhaps try to create there own methods? How dose reading about a method that returns the memory capacity of a string help me with making an efficient method or are you implying its good general knowledge? Regardless, I posted this method hoping someone would show me a more efficient method, Can you do so? Regards, robNO.
Niklas L 5-Feb-11 5:11am    
I will try to make this as short as possible. The main efficiency problem in your implementation is the hidden reallocation of memory in string.operator+=(). It will reallocate memory multiple times, and the longer the string, the more it will have to reallocate. To avoid this, you should use string.reserve() with (str.lenght() - end-start) as an argument before trying to add any characters. I thought string.capacity() would have given you a hint there. Of course the most efficient method would be to do it all in-place. Using any of these two methods will yield a faster function. So, yes, I can write a more efficient method, but no, I won't, because we have string.erase(). About the function signature, if you need indexes, you should use the proper string::size_type, and not unsigned, to avoid portability issues. Could you elaborate on why those indexes are accepted by (const) reference and not by value?

"You don't see the point in giving the original poster an idea to perhaps try to create there own methods?" Reinventing the wheel isn't my deal. Why encourage someone to write something that is already there (and a lot less efficient)? One strength of a programmer is not if he knows how t write a function, but if he knows if he has to. I do this every now and then, and it's really annoying when you find out it has already been done, knowing that you spent extra time on it and maybe even introduced hard to find bugs or performance bottlenecks.
RobNO 5-Feb-11 11:25am    
Thanks for your reply!!!

I am aware that reserving memory is faster because the memory does not need to continuously re-allocate memory, however, considering that I wrote that method in less than 2 minutes and never put any though into efficiency, I would have to say its alright. You are also right that I should have used the string classes typedef for string size, I should have put a little more thought into it, but as previously stated I just threw a random idea out. "Could you elaborate on why those indexes are accepted by "(const) reference and not by value?" Yes, their value does not change and I did not want to reallocate the memory for them in the method.

"Why encourage someone to write something that is already there (and a lot less efficient)? " Well, In a way this can be considered Philosophical, however, I do not want to write about anything unrelated to coding. So with that in mind. As a student I believe rewriting code that is already available helps coders learn to improve by coding more, learning there flaws and most importantly becoming creative.

"One strength of a programmer is not if he knows how t write a function, but if he knows if he has to." True!! It already written why waste the time re-writing it, however, another strength for someone learning a language is to learn to rewrite the code by memory,nearly as efficient as the professional who wrote the library; only if they have extra time. Doing that I believe would help with any future projects.

Furthermore, I do not always do this, however, If i have free time I will.

Thanks for your time,
robNO.
Niklas L 5-Feb-11 19:12pm    
"I did not want to reallocate the memory for them in the method." You do realize that allocating space for a parameter or local variable comes free of charge, and instead of pushing a simple value onto the stack, you are pushing a reference (an address)? So question remains: What's the gain?
You can use following user define function for deleting specific charactor from string.

Function Call :
char *pStr;
pStr = RemoveChar("Ayer estaba solo",0)  // To remove 0th character
pStr = RemoveChar("Ayer estaba solo",4)  // To remove 4th character 


Function Defination:
char* RemoveChar(char str[], int nChPos)
{
  char *pString = str;
  char *pTmpStr;
  int index =0;

  for(int i=0; i<nChPos; i++,index++)
    *(pTmpStr+index) = *(pString+index);

 index++;
 while (*(pString+index) !='\0')
 {
   *(pTmpStr+index-1) = *(pString+index);
   index++;
 }
 *(pTmpStr+index-1) = '\0;
  return pTmpStr;
}
 
Share this answer
 
v5
Comments
Niklas L 3-Feb-11 9:15am    
Are you serious? This function contains more issues than it has code lines.
* 20 elements in result array? Come on.
* Never returning any result.
* Algorithm is all wrong.
* Unnecessary function calls.
Niklas L 4-Feb-11 4:20am    
Two comments on Revision 5 if you're interested. pTmpStr does most certainly not point to writable memory, and the i variable is unnecessary since index has the same value.
LaxmikantYadav 4-Feb-11 4:54am    
Niklas thx for ur valuable comments.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900