String replacement
Posted by Chad Humphries over 2 years ago
Never thought i would put something from PHP into C++.

// Returns a string with all occurrences of search in subject replaced with the given replace string.

std::string str_replace(const std::string& search, const std::string& replace, std::string subject)
{
	int pos = subject.find(search, 0);
	while(pos != std::string::npos)
	{
		subject.erase(pos, search.length());
		subject.insert(pos, replace);
		pos = subject.find(search, pos + replace.length());
	}
	return subject;
}
Language C++ / Tagged with strings