zoukankan      html  css  js  c++  java
  • SQL数据插入字符串时转义函数

    函数一:

     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");

    字符串中有单引号

  • 相关阅读:
    (水题)洛谷
    (水题)洛谷
    洛谷
    (水题)洛谷
    POJ
    poj 3061(二分 or 尺取法)
    poj 2456(二分)
    poj 1064(二分答案)
    POJ 2559(单调栈)
    STL
  • 原文地址:https://www.cnblogs.com/chechen/p/4448127.html
Copyright © 2011-2022 走看看