How to find and replace all occurrences of a substring in a string?

前端 未结 7 1510
无人共我
无人共我 2020-12-31 06:49

I need to search a string and edit the formatting of it.

So far I can replace the first occurrence of the string, but I am unable to do so with the next occurrences

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-31 07:23

    In case boost is available, you can use the following:

    std::string origStr = "this string has *A and then another *A";
    std::string subStringToRemove = "*A";
    std::string subStringToReplace = "[A]";
    
    boost::replace_all(origStr , subStringToRemove , subStringToReplace);
    

    To perform the modification on the original string, OR

    std::string result = boost::replace_all_copy(origStr , subStringToRemove , subStringToReplace);
    

    To perform the modifications without modifying the original string.

提交回复
热议问题