Objective: I have some string stored within double quote which i need to replace with some other input if text match.
see my working code first where i used Replace function.
string replaced = "";
string actualformula = "\"Operational Metrics~Mainline ASM\"/\"Operational Metrics~Mainline Fuel Consumption (In Gallons)\"";
replaced = actualformula.Replace("Operational Metrics~Mainline ASM", "AA10");
replaced= actualformula.Replace("Operational Metrics~Mainline Fuel Consumption (In Gallons)", "BB10");
the above code perfectly replace.
see now the same code where i am trying to replace with custom extension method which not able to replace this text Operational Metrics~Mainline Fuel Consumption (In Gallons) with BB10
see extension method code
public static class Extension
{
public static string ReplaceWholeWord(this string original, string wordToFind, string replacement, RegexOptions regexOptions = RegexOptions.None)
{
string pattern = String.Format(@"\b{0}\b", wordToFind);
string ret = Regex.Replace(original, pattern, replacement, regexOptions);
return ret;
}
}
see actual code where i am using ReplaceWholeWord extension method which not able to replace
this text Operational Metrics~Mainline Fuel Consumption (In Gallons) with BB10
private void button2_Click(object sender, EventArgs e)
{
string replaced = "";
string actualformula = "\"Operational Metrics~Mainline ASM\"/\"Operational Metrics~Mainline Fuel Consumption (In Gallons)\"";
replaced = actualformula.ReplaceWholeWord("Operational Metrics~Mainline ASM", "AA10");
replaced= actualformula.ReplaceWholeWord("Operational Metrics~Mainline Fuel Consumption (In Gallons)", "BB10");
}
please guide me what is wrong in my approach for which code not able to replace
this text Operational Metrics~Mainline Fuel Consumption (In Gallons) with BB10
Tried this way also did not worked.
private void button2_Click(object sender, EventArgs e)
{
string replaced = "";
//string actualformula = ""Operational Metrics~Mainline ASM"/"Operational Metrics~Mainline Fuel Consumption (In Gallons)"";
string actualformula = """+ "Operational Metrics~Mainline ASM" + """ + "/"+ """+"Operational Metrics~Mainline Fuel Consumption (In Gallons)"+ """;
replaced = actualformula.ReplaceWholeWord("Operational Metrics~Mainline ASM", "AA10");
replaced= actualformula.ReplaceWholeWord("Operational Metrics~Mainline Fuel Consumption (In Gallons)", "BB10");
}
Thanks