我正在研究为字符串中的每个'添加单引号'的东西。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string insertStatement = "INSERT INTO EXCEPTION_LOG (value1, value2" +
"VALUES ('test', 'test2');";
AddEscapeStrings(insertStatement);
Console.Read();
}
private static void AddEscapeStrings(string insertStatement)
{
int i = 0;
char[] c = insertStatement.ToCharArray();
while (i < c.Length)
{
if (c.ElementAt(i) == '\'')
{
Console.Write(i + ", ");
c[i - 1] = '\'';
}
i++;
Console.WriteLine("\r\n ");
for (int j = 0; j < c.Length; j++ )
{
Console.Write(c.GetValue(j));
}
}
}
}
}
这段代码正在查找'的所有位置,现在我想在其前面添加一个额外的'。但是我不确定哪种方法是最好的,c#是否有一种在所需位置添加'而不替换该位置当前字符的方法,还是我每次都必须移动数组?
期望的结果将是(请注意双单引号):
“在EXCEPTION_LOG中插入(值1,值2”)+
“ VALUES(” test“,” test2“);”;
最佳答案
也许我缺少了一些东西,但是为什么不简单地使用Replace方法呢?
insertStatement = insertStatement.Replace("'","''");
关于c# - C#-程序添加一个额外的'找到'的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7414961/