替换x和x+之间的字符串的一部分;1发生率c#

替换x和x+之间的字符串的一部分;1发生率c#,c#,string,replace,find-occurrences,C#,String,Replace,Find Occurrences,我的密码 string sql = "INSERT INTO TABLE1(col1, col2, col3, col4, col5) VALUES (, NULL, NULL, NULL, NULL, NULL,)"; //Insert value for the third column(between the 3rd and 4th comma) Regex rgx = new Regex(@", null"); sql = rgx.Replace(sql, ", 'abc'", 3);

我的密码

string sql = "INSERT INTO TABLE1(col1, col2, col3, col4, col5) VALUES (, NULL, NULL, NULL, NULL, NULL,)";
//Insert value for the third column(between the 3rd and 4th comma)
Regex rgx = new Regex(@", null"); 
sql = rgx.Replace(sql, ", 'abc'", 3);// this doesn't work
sql = rgx.Replace(sql, ", 'def'", 4);// second insert
期望结果

sql = "INSERT INTO TABLE1(col1, col2, col3, col4, col5) VALUES (, NULL, NULL, 'abc', 'def', NULL,)";
//Then I'll remove the first and last comma between the VALUES parenthesis.

MatchEvaluator只是一个委托。你传递你的函数


MatchEvaluator只是一个委托。你传递你的函数

像这样使用

string sql = "INSERT INTO TABLE1(col1, col2, col3, col4, col5) VALUES (,NULL, NULL, NULL, NULL, NULL,)";

string nulls = "NULL";
List<int> indexes = new List<int>();
foreach (Match match in Regex.Matches(sql, nulls))
{
     indexes.Add(match.Index);
}

sql = sql.Remove(indexes[2], 4).Insert(indexes[2], "'abc'");
Console.WriteLine(sql);
作为解释,这将在您的查询中找到第三个
NULL
,然后将其自身替换为
'abc'

这里有一个。

像这样使用

string sql = "INSERT INTO TABLE1(col1, col2, col3, col4, col5) VALUES (,NULL, NULL, NULL, NULL, NULL,)";

string nulls = "NULL";
List<int> indexes = new List<int>();
foreach (Match match in Regex.Matches(sql, nulls))
{
     indexes.Add(match.Index);
}

sql = sql.Remove(indexes[2], 4).Insert(indexes[2], "'abc'");
Console.WriteLine(sql);
作为解释,这将在您的查询中找到第三个
NULL
,然后将其自身替换为
'abc'

这里有一个。

关于
//然后我将删除第一个和最后一个逗号。

sql = sql.Replace("(,", "(").Replace(",)", ")");

关于
//然后我将删除第一个和最后一个逗号。

sql = sql.Replace("(,", "(").Replace(",)", ")");

您可以使用此扩展方法。的修改版本

这样称呼它

string sql = "INSERT INTO TABLE1(col1, col2, col3, col4, col5) VALUES (, NULL, NULL, NULL, NULL, NULL,)";
sql = sql.ReplaceSpecifiedIndex("null", "abc", 3);

您可以使用此扩展方法。的修改版本

这样称呼它

string sql = "INSERT INTO TABLE1(col1, col2, col3, col4, col5) VALUES (, NULL, NULL, NULL, NULL, NULL,)";
sql = sql.ReplaceSpecifiedIndex("null", "abc", 3);
没有正则表达式:

string sql = "INSERT INTO TABLE1(col1, col2, col3, col4, col5) VALUES (, NULL, NULL, NULL, NULL, NULL,)";
int indexOfvalues = sql.IndexOf("VALUES (");
if (indexOfvalues >= 0)
{
    indexOfvalues += "VALUES (".Length;
    int endIndexOfvalues = sql.IndexOf(")", indexOfvalues);
    if (endIndexOfvalues >= 0)
    {
        string sqlValues = sql.Substring(indexOfvalues, endIndexOfvalues - indexOfvalues);
        string[] values = sqlValues.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        if(values.Length >= 3)
            values[2] = "'abc'";
        string newValues = string.Join(",", values);
        sql = string.Format("{0}{1})", sql.Substring(0, indexOfvalues), newValues.Trim());
    }
}
结果:

INSERT INTO TABLE1(col1, col2, col3, col4, col5) VALUES (NULL, NULL, 'abc', NULL, NULL)
或者使用
String.Split
更短、可读性更强(可能有点危险):

没有正则表达式:

string sql = "INSERT INTO TABLE1(col1, col2, col3, col4, col5) VALUES (, NULL, NULL, NULL, NULL, NULL,)";
int indexOfvalues = sql.IndexOf("VALUES (");
if (indexOfvalues >= 0)
{
    indexOfvalues += "VALUES (".Length;
    int endIndexOfvalues = sql.IndexOf(")", indexOfvalues);
    if (endIndexOfvalues >= 0)
    {
        string sqlValues = sql.Substring(indexOfvalues, endIndexOfvalues - indexOfvalues);
        string[] values = sqlValues.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        if(values.Length >= 3)
            values[2] = "'abc'";
        string newValues = string.Join(",", values);
        sql = string.Format("{0}{1})", sql.Substring(0, indexOfvalues), newValues.Trim());
    }
}
结果:

INSERT INTO TABLE1(col1, col2, col3, col4, col5) VALUES (NULL, NULL, 'abc', NULL, NULL)
或者使用
String.Split
更短、可读性更强(可能有点危险):


我从中编辑了
StrExtract
函数,并获得了一个新函数,我将其命名为
StrReplace

public static string StrReplace(string cSearchExpression, string replacement, string cBeginDelim, string cEndDelim, int nBeginOccurence)
    {
        string cstring = cSearchExpression;
        string cb = cBeginDelim;
        string ce = cEndDelim;

        if (cSearchExpression.Contains(cBeginDelim) == false && cSearchExpression.Contains(cEndDelim) == false)
        {
            return cstring;
        }

        //Lookup the position in the string
        int nbpos = At(cb, cstring, nBeginOccurence) + cb.Length - 1;
        int nepos = cstring.IndexOf(ce, nbpos + 1);

        //Reaplce the part of the string if we get it right
        if (nepos > nbpos)
        {
            cstring = cstring.Remove(nbpos, nepos - nbpos).Insert(nbpos, replacement);
        }
        return cstring;
    }
At
功能可在工具箱中找到

sql = strings.StrReplace(sql , " abc", ",", ",", 3);
sql = strings.StrReplace(sql , " def", ",", ",", 4);

我从中编辑了
StrExtract
函数,并获得了一个新函数,我将其命名为
StrReplace

public static string StrReplace(string cSearchExpression, string replacement, string cBeginDelim, string cEndDelim, int nBeginOccurence)
    {
        string cstring = cSearchExpression;
        string cb = cBeginDelim;
        string ce = cEndDelim;

        if (cSearchExpression.Contains(cBeginDelim) == false && cSearchExpression.Contains(cEndDelim) == false)
        {
            return cstring;
        }

        //Lookup the position in the string
        int nbpos = At(cb, cstring, nBeginOccurence) + cb.Length - 1;
        int nepos = cstring.IndexOf(ce, nbpos + 1);

        //Reaplce the part of the string if we get it right
        if (nepos > nbpos)
        {
            cstring = cstring.Remove(nbpos, nepos - nbpos).Insert(nbpos, replacement);
        }
        return cstring;
    }
At
功能可在工具箱中找到

sql = strings.StrReplace(sql , " abc", ",", ",", 3);
sql = strings.StrReplace(sql , " def", ",", ",", 4);

“//然后我将删除第一个和最后一个逗号”这是这个问题的一部分还是你的下一个问题?你从中得到的实际结果是什么?乍一看,我不认为你可以这样使用正则表达式(尽管我对它们的了解有限)@Tim我提到,由于SQL语法“/,我将删除第一个和最后一个逗号”这是这个问题的一部分还是你的下一个问题?你实际从中得到了什么结果?乍一看,我认为您不能以这种方式使用RegEx(尽管我对它们的了解有限)@Tim我提到过,因为SQL语法是一个很好的例子,但是。。。当我想进行第二次插入时会发生什么?第一次插入后,索引列表不再对应。@Misi您可以更改第
sql=sql.Remove(索引[2],4)行中的索引。插入(索引[2],“abc”)
但是请记住,当您第一次插入
'abc'
索引时,可以从
0
3
,因为您的sql中有4
NULL
而不是5。这是一个很好的示例,但是。。。当我想进行第二次插入时会发生什么?第一次插入后,索引列表不再对应。@Misi您可以更改第
sql=sql.Remove(索引[2],4)行中的索引。插入(索引[2],“abc”)
但是请记住,当您插入第一个
'abc'
索引时,可以从
0
3
,因为sql中有4个
NULL