Reputation: 2143
I have a long string as below. I would like to replace some character when it found some keyword (.abc_ or .ABC_)
. As the system read line by line, if the keyword is found, then it will replace the word infront to become "john"
insert into material.abc_Inventory; Delete * from table A; ....
insert into job.ABC_Inventory; Show select .....; ....
Changed to
insert into john.ABC_Inventory; Delete * from table A;
insert into john.ABC_Inventory; Show select .....;
Below is my code.
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();
}
}
I am not able to get the word material or job in front of the keyword.
Upvotes: 1
Views: 284
Reputation: 12184
content = Regex.Replace(content, @"\s\w+\.(abc|ABC)_", " john.$1_");
Upvotes: 3
Reputation: 75316
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");
}
Upvotes: 1
Reputation: 100545
Use String.Format instead of this:
var stringBuffer = new StringBuffer();
...
line = "insert into {0}.ABC_Inventory; Show select ..."
stringBuffer.AppendFormat(line, arg1, arg2, arg3);
...
Upvotes: 1