Maybe I'm losing sight of something obvious, but I was wondering what might be the fastest way to implement replacing whole words with C ++. At first, I looked at just concatenating spaces in a search word, but that doesn't take into account line boundaries or punctuation marks.
This is my current abstraction to replace (not a whole word):
void Replace(wstring& input, wstring find, wstring replace_with) {
if (find.empty() || find == replace_with || input.length() < find.length()) {
return;
}
for (size_t pos = input.find(find);
pos != wstring::npos;
pos = input.find(find, pos)) {
input.replace(pos, find.length(), replace_with);
pos += replace_with.length();
}
}
If I consider spaces as the boundary of a word, I could implement this by comparing the beginning and end of the search string with the search string to cover the borders of the strings, and then follow with the replacement (L '' + find + L '') .... but I was wondering if there was a more elegant solution that would effectively include punctuation.
, ( , !) # $% & '() * +, -./at minimum - (c > 31 && c < 48)).
, Unicode, . , STL .
- , , .