使用C#将所有引号设置为双引号

JP.*_*..t 1 c# quote double-quotes

我尝试让用户可以在CustomerName中搜索带引号的客户.

用户在customerNameTextBox中搜索设置为customerNameTB的用户.

如果用户使用引号('),它将被替换为双引号.

如果有三重引号('''),它将被替换为双引号.

这是我的代码:

string customerNameTB = customerNameTextbox.Text;                
customerNameTB.Replace("'", "''");                
while (customerNameTB.Contains("'''"))
{
   customerNameTB.Replace("'''", "''");
}
Run Code Online (Sandbox Code Playgroud)

此代码后的结果是引号仍是单引号.

这条小代码有什么问题.

在答案后编辑

我的代码应如下所示:

 string customerNameTB = customerNameTextbox.Text;                
    customerNameTB = customerNameTB.Replace("'", "''");                
    while (customerNameTB.Contains("'''"))
    {
       customerNameTB = customerNameTB.Replace("'''", "''");
    }
Run Code Online (Sandbox Code Playgroud)

bkr*_*bbs 5

你很亲密!这就是你需要的:

string customerNameTB = customerNameTextbox.Text;  
// single quotes
customerNameTB = customerNameTB.Replace("'", "''");
// triple quotes
customerNameTB = customerNameTB.Replace("'''", "''");
Run Code Online (Sandbox Code Playgroud)

替换不会在原始字符串中替换它,它会返回一个新的字符串,您必须将其分配给某个东西,否则它将被丢弃.