Answers for "how to delete element from string"

12

erasing a character from a string in c++

// string::erase
#include <iostream>
#include <string>

int main ()
{
  std::string str ("This is an example sentence.");
  std::cout << str << 'n';
                                           // "This is an example sentence."
  str.erase (10,8);                        //            ^^^^^^^^
  std::cout << str << 'n';
                                           // "This is an sentence."
  str.erase (str.begin()+9);               //           ^
  std::cout << str << 'n';
                                           // "This is a sentence."
  str.erase (str.begin()+5, str.end()-9);  //       ^^^^^
  std::cout << str << 'n';
                                           // "This sentence."
  return 0;
}
Posted by: Guest on May-13-2020
19

remove from string python

"Str*ing With Chars I! don't want".replace('!','').replace('*','')
Posted by: Guest on May-13-2020
-1

take a string and a character, remove that character from string

cpp code
int main()
{
    string str;
    getline(cin,str);
    char c;
    cin>>c;
    
    string temp="";
    
    for(int i=0; i<str.size(); i++){
        
        if(str[i] == c)
          continue;
        temp += str[i];  
        
    }
    
    cout<<temp;

    return 0;
}
Posted by: Guest on August-11-2021

Code answers related to "how to delete element from string"

Python Answers by Framework

Browse Popular Code Answers by Language