函数一:
1 std::string CheckString(std::string& strSource) 2 { 3 std::string strOldValue = "'"; 4 std::string strNewValue = "''"; 5 for(std::string::size_type pos(0); pos != std::string::npos; pos += strNewValue.length()) 6 { 7 if ((pos = strSource.find(strOldValue, pos)) != std::string::npos) 8 { 9 strSource.replace(pos, strOldValue.length(), strNewValue); 10 } 11 else 12 { 13 break; 14 } 15 } 16 return strSource; 17 }
方法二:
1 std::string& replace_all_distinct(std::string& str,const std::string& old_value,const std::string& new_value) 2 { 3 for(std::string::size_type pos(0); pos!=std::string::npos; pos+=new_value.length()) 4 { 5 if((pos=str.find(old_value,pos)) != std::string::npos) 6 str.replace(pos,old_value.length(),new_value); 7 else 8 { 9 break; 10 } 11 } 12 return str; 13 }
使用:
例如:
replace_all_distinct("sub'ject","'","''"); CheckString("subj'e'ct");
字符串中有单引号