c++ replace n 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]
*/
string ReplaceFirstNSubstringOccurrences(string sAll, string sStringToRemove, string sStringToInsert, int iN)
{
// get the length
int iLength = sStringToRemove.length();
size_t index = 0;
int counter = 1;
while (counter <= iN)
{
// 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;
counter++;
}
return sAll;
}
// EXAMPLE: in usage
string sInitialString = "Replace this, and also this, but not this at the end";
string sFinalString = ReplaceFirstNSubstringOccurrences(sInitialString, "this", "{new}",2);
cout << "[sInitialString->" << sInitialString << "]\n";
cout << "[sFinalString->" << sFinalString << "]\n";
/* OUTPUT:
[sInitialString->Replace this, and also this, but not this at the end]
[sFinalString->Replace {new}, and also {new}, but not this at the end]
*/
Also in C++:
- Title
- else if c++
- Category
- C++
- Title
- unordered_set in c++ and ordered set diff
- Category
- C++
- Title
- hohw toparse a string in c++
- Category
- C++
- Title
- c++ give options
- Category
- C++
- Title
- singleton c++
- Category
- C++
- Title
- friend function in c++
- Category
- C++
- Title
- c++ template function
- Category
- C++
- Title
- check for bst
- Category
- C++
- Title
- how to use max_element in c++ with vector
- Category
- C++
- Title
- c++ char define
- Category
- C++
- Title
- power c++
- Category
- C++
- Title
- class in c++
- Category
- C++
- Title
- initialising 2d vector
- Category
- C++
- Title
- how to check datatype of a variable in c++
- Category
- C++
- Title
- variadic templates
- Category
- C++
- Title
- C++ Student::Student()
- Category
- C++
- Title
- how to delete an element in vector pair in cpp
- Category
- C++
- Title
- first fit algorithm
- Category
- C++
- Title
- pionter in c++
- Category
- C++
- Title
- rosrun actionlib_msgs genaction.py
- Category
- C++
- Title
- how print fload wiht 3 decimal in c++
- Category
- C++
- Title
- sort function in c++
- Category
- C++
- Title
- create copy of range of string c++
- Category
- C++
- Title
- random number generator c++
- Category
- C++
- Title
- initialize map c++
- Category
- C++
- Title
- delete 2d dynamic array c++
- Category
- C++
- Title
- c++ string to int
- Category
- C++
- Title
- delete files c++
- Category
- C++
- Title
- c++ bsod
- Category
- C++
- Title
- how to print a decimal number upto 6 places of decimal in c++
- Category
- C++
- Title
- min and max heap in cpp
- Category
- C++
- Title
- c++ operator overloading
- Category
- C++
- Title
- generate random uniform distribution c++
- Category
- C++
- Title
- c++ iterate through constant list
- Category
- C++
- Title
- eigenvalue of matrix c++ using Eigen
- Category
- C++
- Title
- lopping over an array c++
- Category
- C++
- Title
- accumulate c++ stl
- Category
- C++
- Title
- c++ vector constructors
- Category
- C++
- Title
- how to find hcf in c++
- Category
- C++
- Title
- arduino for command
- Category
- C++
- Title
- calling a method on an object c++
- Category
- C++
- Title
- c++ remove text file
- Category
- C++
- Title
- lisy stl C++
- Category
- C++
- Title
- flushing output in c++
- Category
- C++
- Title
- print type cpp
- Category
- C++
- Title
- c++ multiple inheritance diamond problem
- Category
- C++
- Title
- tree traversal c++ in order
- Category
- C++
- Title
- create a 2d array c++
- Category
- C++
- Title
- how to turn int into string c++
- Category
- C++
- Title
- how to convert qt string to string
- Category
- C++
- Title
- c++ create button
- Category
- C++
- Title
- E/flutter (20384): [ERROR:flutter/third_party/txt/src/minikin/FontFamily.cpp(184)] Could not get cmap table size! E/flutter (20384): F/flutter (20384): [FATAL:flutter/third_party/txt/src/minikin/FontCollection.cpp(95)] nTypefaces == 0
- Category
- C++
- Title
- c++ read_ascii
- Category
- C++
- Title
- how to format decimal palces in c++
- Category
- C++
- Title
- newline in c++
- Category
- C++
- Title
- COunt the number of continous subsequences such that the sum is between
- Category
- C++
- Title
- vector in c++ class
- Category
- C++
- Title
- how to print eachh chars in string data type in c++
- Category
- C++
- Title
- c++ replace n substrings
- Category
- C++
- Title
- : error: ‘cont’ cannot be used as a function return (cont(cont-1))/2;
- Category
- C++
- Title
- timer in c++
- Category
- C++
- Title
- c++ vector size
- Category
- C++
- Title
- residuo en lenguaje c
- Category
- C++
- Title
- level order traversal
- Category
- C++
- Title
- c++ class template
- Category
- C++
- Title
- c++ create array
- Category
- C++
- Title
- vector stl c++
- Category
- C++
- Title
- how to make loop in c++
- Category
- C++
- Title
- substr c++
- Category
- C++
- Title
- c++ string to vector int
- Category
- C++
- Title
- 2d vector
- Category
- C++
- Title
- c++ split at character
- Category
- C++
- Title
- matrix multiplication c++ eigen
- Category
- C++
- Title
- qt make widget ignore mouse events
- Category
- C++
- Title
- how to know the correct class of objects cpp
- Category
- C++
- Title
- convert char to string - c++
- Category
- C++
- Title
- restting a queue stl
- Category
- C++
- Title
- c++ looping through a vector
- Category
- C++
- Title
- set in c++
- Category
- C++
- Title
- arduino delay millis
- Category
- C++
- Title
- c++ switch
- Category
- C++
- Title
- how to find the number of cycles in a graph C++
- Category
- C++
- Title
- Application of c++ in youtube program
- Category
- C++
- Title
- C++ If
- Category
- C++
- Title
- checking an int in c++
- Category
- C++
- Title
- how to declare a vector in c++
- Category
- C++
- Title
- min coin change problem dp
- Category
- C++
- Title
- intersection between vector c++
- Category
- C++
- Title
- find in string c++
- Category
- C++
- Title
- Find a element in a map C++
- Category
- C++
- Title
- goto c++
- Category
- C++
- Title
- c++ how to make a negative float positive
- Category
- C++
- Title
- ue4 c++ overlapping functions cpp setup
- Category
- C++
- Title
- git branch in my bash prompt
- Category
- C++
- Title
- prims c++
- Category
- C++
- Title
- bellman ford algorithm cp algorithm
- Category
- C++
- Title
- gta san andreas
- Category
- C++
- Title
- what is a header in c++
- Category
- C++
- Title
- chess perft 5
- Category
- C++
- Title
- c++ transform
- Category
- C++