【发布时间】:2010-03-08 12:41:51
【问题描述】:
.net 是否具有 Delphi 的 QuotedStr 函数的等价物。这会将所有引号替换为两个引号字符,然后在开头和结尾添加引号,例如
假设变量 s 包含字符串:
Welcome to Steve's Store
然后QuotedStr(s) 会返回:
'Welcome to Steve''s Store'
【问题讨论】:
.net 是否具有 Delphi 的 QuotedStr 函数的等价物。这会将所有引号替换为两个引号字符,然后在开头和结尾添加引号,例如
假设变量 s 包含字符串:
Welcome to Steve's Store
然后QuotedStr(s) 会返回:
'Welcome to Steve''s Store'
【问题讨论】:
像这样:
"'" + str.Replace("'", "''") + "'"
【讨论】:
/// <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("'", "''") + "'";
}
【讨论】: