【问题标题】:.net equivilent of Delphi's QuotedStr function.net 相当于 Delphi QuotedStr 函数
【发布时间】:2010-03-08 12:41:51
【问题描述】:

.net 是否具有 Delphi 的 QuotedStr 函数的等价物。这会将所有引号替换为两个引号字符,然后在开头和结尾添加引号,例如

假设变量 s 包含字符串:

Welcome to Steve's Store

然后QuotedStr(s) 会返回:

'Welcome to Steve''s Store'

【问题讨论】:

    标签: c# .net delphi


    【解决方案1】:

    像这样:

    "'" + str.Replace("'", "''") + "'"
    

    【讨论】:

    • 不,这与SQL无关
    • 谢谢,所以没有内置函数?
    • 是的,没有内置函数
    • 但是,假设您使用的是 .Net 3.5 +,您可以为此创建一个帮助扩展方法
    【解决方案2】:
    /// <summary>
    /// Use QuotedStr to convert the string S to a quoted string. 
    /// A single quotation mark (') is inserted at the beginning and end of S, 
    /// and each single quotation mark in the string is repeated. 
    /// </summary>
    String QuotedStr(String s)
    {
        //Note: All code on stackoverflow is public domain; no attribution required.
    
        //Handle the case when s is null.
        if (String.IsNullOrEmpty(s))
            return "''";
       
        return "'" + s.Replace("'", "''") + "'";
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-28
      • 1970-01-01
      • 2023-01-27
      • 2011-02-04
      • 1970-01-01
      • 1970-01-01
      • 2013-03-14
      • 2012-06-16
      • 1970-01-01
      相关资源
      最近更新 更多