c++ replace substrings
C++
using namespace std;
string ReplaceAllSubstringOccurrences(string sAll, string sStringToRemove, string sStringToInsert)
{
int iLength = sStringToRemove.length();
size_t index = 0;
while (true)
{
/* Locate the substring to replace. */
index = sAll.find(sStringToRemove, index);
if (index == std::string::npos)
break;
/* Make the replacement. */
sAll.replace(index, iLength, sStringToInsert);
/* Advance index forward so the next iteration doesn't pick it up as well. */
index += iLength;
}
return sAll;
}
// EXAMPLE: in usage
string sInitialString = "Replace this, and also this, don't forget this too";
string sFinalString = ReplaceAllSubstringOccurrences(sInitialString, "this", "{new word/phrase}");
cout << "[sInitialString->" << sInitialString << "]\n";
cout << "[sFinalString->" << sFinalString << "]\n";
/* OUTPUT:
[sInitialString->Replace this, and also this, don't forget this too]
[sFinalString->Replace {new word/phrase}, and also {new word/phrase}, don't forget {new word/phrase} too]
*/
Also in C++:
- Title
- Temporary file using MSFT API in cpp
- Category
- C++
- Title
- tokenize string c++
- Category
- C++
- Title
- error: redefinition of ‘class Customer’
- Category
- C++
- Title
- c++ crash windows
- Category
- C++
- Title
- vector initialization c++
- Category
- C++
- Title
- c++ code for polynomial addition
- Category
- C++
- Title
- constant variables in c++
- Category
- C++
- Title
- c++ vector iterator
- Category
- C++
- Title
- restting a queue stl
- Category
- C++
- Title
- how to sort an array c++
- Category
- C++
- Title
- initialise 2d vector in c++
- Category
- C++
- Title
- maximum possible number atmost k swaps
- Category
- C++
- Title
- how to find hcf in c++
- Category
- C++
- Title
- find in string c++
- Category
- C++
- Title
- c++ allocate dynamic with initial values
- Category
- C++
- Title
- arrays in C++
- Category
- C++
- Title
- built in function in c++ for binary to decimal
- Category
- C++
- Title
- cannot jump from switch statement to this case label c++
- Category
- C++
- Title
- how to take input in C++ in coding
- Category
- C++
- Title
- command line options in c++
- Category
- C++
- Title
- remove item from layout
- Category
- C++
- Title
- Html tabulation
- Category
- C++
- Title
- jump to case label c++
- Category
- C++
- Title
- how can make string value in cpp
- Category
- C++
- Title
- cut by delimiter c++
- Category
- C++
- Title
- string substr c++
- Category
- C++
- Title
- nan c++ example
- Category
- C++
- Title
- system("pause") note working c++
- Category
- C++
- Title
- how to get os name in c++
- Category
- C++
- Title
- unordered_map c++
- Category
- C++
- Title
- c++ calculator program using switch case
- Category
- C++
- Title
- c++ push multiple elements to vector
- Category
- C++
- Title
- screen record ios simulator
- Category
- C++
- Title
- qt make widget ignore mouse events
- Category
- C++
- Title
- c++ append to list
- Category
- C++
- Title
- c++ yes no question
- Category
- C++
- Title
- change int to string cpp
- Category
- C++
- Title
- Find the duplicate in an array of N integers.
- Category
- C++
- Title
- how to declare a vector in c++
- Category
- C++
- Title
- can you use a return to print a string when referencing an integer c++
- Category
- C++
- Title
- c++ client service ros
- Category
- C++
- Title
- iterative inorder traversal
- Category
- C++
- Title
- Check if a Number is Odd or Even using Bitwise Operators
- Category
- C++
- Title
- euler's totient function c++
- Category
- C++
- Title
- else if c++
- Category
- C++
- Title
- retu7rn this c++
- Category
- C++
- Title
- lopping over an array c++
- Category
- C++
- Title
- random number generator c++
- Category
- C++
- Title
- creare array con c++
- Category
- C++
- Title
- c++ read file line by line
- Category
- C++
- Title
- c++ reading string
- Category
- C++
- Title
- first prime numbers less than
- Category
- C++
- Title
- COunt the number of continous subsequences such that the sum is between
- Category
- C++
- Title
- initialize 3d vector c++
- Category
- C++
- Title
- C++ and endl
- Category
- C++
- Title
- log base e synthax c++
- Category
- C++
- Title
- cpp nan value
- Category
- C++
- Title
- int random string generator c++
- Category
- C++
- Title
- c++ vector add element
- Category
- C++
- Title
- sort function in cpp
- Category
- C++
- Title
- calculate factorial
- Category
- C++
- Title
- program to know if a number is prime
- Category
- C++
- Title
- COnvert string to char * C++
- Category
- C++
- Title
- iterar un map c++
- Category
- C++
- Title
- c++ char to string
- Category
- C++
- Title
- calling by reference c++
- Category
- C++
- Title
- c++ find prime numbers
- Category
- C++
- Title
- stl sort in c++
- Category
- C++
- Title
- private and public in namespace cpp
- Category
- C++
- Title
- double max value c++
- Category
- C++
- Title
- c++ sort vector of objects by property
- Category
- C++
- Title
- syntax c++
- Category
- C++
- Title
- sieve of eratosthenes c++
- Category
- C++
- Title
- c++ char print align
- Category
- C++
- Title
- min in vector c++
- Category
- C++
- Title
- never gonna give you up lyrics
- Category
- C++
- Title
- how to find the index of an element in a vector c++
- Category
- C++
- Title
- string input
- Category
- C++
- Title
- cheap hosting
- Category
- C++
- Title
- how to use wasd c++
- Category
- C++
- Title
- how to append one vector to another c++
- Category
- C++
- Title
- minmax_element c++
- Category
- C++
- Title
- c++ ros subscriber
- Category
- C++
- Title
- tuple c++
- Category
- C++
- Title
- array<string, 7> c++
- Category
- C++
- Title
- case label in c++
- Category
- C++
- Title
- passing reference in c++
- Category
- C++
- Title
- c++ vector size
- Category
- C++
- Title
- C++ Syntax
- Category
- C++
- Title
- solve linear equations geeksforgeeks
- Category
- C++
- Title
- c++ cli convert string to string^
- Category
- C++
- Title
- memcmp in cpp
- Category
- C++
- Title
- c++ function to find length of array
- Category
- C++
- Title
- std::iomanip c++
- Category
- C++
- Title
- c++ triple
- Category
- C++
- Title
- how do you add two random numbers in c++
- Category
- C++
- Title
- first prime numbers
- Category
- C++
- Title
- how to get the prime number in c++ where time complexity is 0(log n)
- Category
- C++
- Title
- c++ initialise array
- Category
- C++
- Title
- basic cpp programs
- Category
- C++