1

我有一个长字符串,如下所示。当它找到一些关键字时,我想替换一些字符(.abc_ or .ABC_)。当系统逐行读取时,如果找到关键字,则将替换前面的单词变成"john"

insert into material.abc_Inventory; Delete * from table A; ....   
insert into job.ABC_Inventory; Show select .....; ....

变成

insert into john.ABC_Inventory; Delete * from table A;    
insert into john.ABC_Inventory; Show select .....;

下面是我的代码。

string output = string.Empty;
using (StringReader reader = new StringReader(content))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        if (line.Contains(".ABC_"))
            line.Replace(" word in front of the keyword" + line.Substring(line.IndexOf(".ABC_")), " john" + line.Substring(line.IndexOf(".ABC_")));
            output += whole line of edited code; 

        else if (line.Contains(".abc_"))
            line.Replace(" word in front of the keyword" + line.Substring(line.IndexOf(".abc_")), " john" + line.Substring(line.IndexOf(".abc_")));
            output += whole line of edited code; 

        else
            output += line.ToString();
    }
}

我无法在关键字前面获得材料或工作一词。

4

3 回答 3

3
content =  Regex.Replace(content, @"\s\w+\.(abc|ABC)_", " john.$1_");
于 2012-09-13T07:18:39.580 回答
1
var list = line.Split(new[] {".abc_", ".ABC_"}, 
                              StringSplitOptions.RemoveEmptyEntries);
if (list.Count() > 1)
{
    string toReplace = list.First().Split(' ').Last();
    string output = line.Replace(toReplace, "john");
}
于 2012-09-13T07:14:34.553 回答
1

使用 String.Format 而不是这个:

var stringBuffer = new StringBuffer();
...
line = "insert into {0}.ABC_Inventory; Show select ..."
stringBuffer.AppendFormat(line, arg1, arg2, arg3);
...
于 2012-09-13T07:13:30.720 回答