Answers for "delete parts of a string c++"

9

removing a character from a string in c++

#include<iostream>
#include<algorithm>

using namespace std;
main() {
   string my_str = "ABAABACCABA";

   cout << "Initial string: " << my_str << endl;

   my_str.erase(remove(my_str.begin(), my_str.end(), 'A'), my_str.end()); //remove A from string
   cout << "Final string: " << my_str;
}
Posted by: Guest on October-15-2020
0

c++ erase substring

using namespace std;

string BasicString = "I dont like erasing strings";
string StrToErase = " dont";

string::size_type i = BasicString.find(StrToErase);

if (i != string::npos)
{
   BasicString.erase(i, StrToErase.length()); 
}

cout << BasicString << "\n"; // OUTPUT: I like erasing strings
Posted by: Guest on May-14-2021
0

delete parts of a string c++

#include<iostream>
#include<string>

using namespace std;
int main()
{
   string text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

   cout << "Initial string: " << text << endl;

   text.erase(10, string::npos);
  // string.erase(pos1, pos2) removes the part of the string between position 1 and 2.
  // In this case, between character 10 and the end of the string.

   cout << "Final string: " << text;
}
Posted by: Guest on July-19-2021
1

erase string c++

string& erase (size_t pos = 0, size_t len = npos);
/*
pos
Position of the first character to be erased.
If this is greater than the string length, it throws out_of_range.
Note: The first character in str is denoted by a value of 0 (not 1).
len
Number of characters to erase (if the string is shorter, as many characters as possible are erased).
A value of string::npos indicates all characters until the end of the string.
*/
Posted by: Guest on February-23-2021

Code answers related to "delete parts of a string c++"

Code answers related to "TypeScript"

Browse Popular Code Answers by Language