C# How to use place holder in string variable
-
see the code how i am creating place holder now. _Yoy has a formula used in excel.
private string _Yoy = @"IF(AND(ISNUMBER(C#),P#>0,NOT(ISERROR(C#/P#))),C#/P#-1,"""")";
public string YoY
{
get { return _Yoy; }
set { _Yoy = value; }
}private void button1\_Click(object sender, EventArgs e) { string strBMFormula = YoY.Replace("P#", "2005.13").Replace("C#", "7777.10"); }
I am replacing P# & C# with some value at runtime and program working as expected but i am doing this in large loop where Replace() function is getting called repeatedly which may increase memory use.
@"IF(AND(ISNUMBER(C#),P#>0,NOT(ISERROR(C#/P#))),C#/P#-1,"""")";
here i gave 4 double quote because there would two double quote in excel function. i am using .Net version 4.5 so please suggest me how to create a place holder in string variable and put my value there without using Replace function. i try this approach too but still no luck.
private void button1_Click(object sender, EventArgs e)
{
string strBMFormula = Replacement("A10", "Z210");
}private string Replacement(string value1, string value2)
{
return @"IF(AND(ISNUMBER({value1}),{value2}>0,NOT(ISERROR({value1}/{value2}))),{value1}/{value2}-1,"""")";
} -
see the code how i am creating place holder now. _Yoy has a formula used in excel.
private string _Yoy = @"IF(AND(ISNUMBER(C#),P#>0,NOT(ISERROR(C#/P#))),C#/P#-1,"""")";
public string YoY
{
get { return _Yoy; }
set { _Yoy = value; }
}private void button1\_Click(object sender, EventArgs e) { string strBMFormula = YoY.Replace("P#", "2005.13").Replace("C#", "7777.10"); }
I am replacing P# & C# with some value at runtime and program working as expected but i am doing this in large loop where Replace() function is getting called repeatedly which may increase memory use.
@"IF(AND(ISNUMBER(C#),P#>0,NOT(ISERROR(C#/P#))),C#/P#-1,"""")";
here i gave 4 double quote because there would two double quote in excel function. i am using .Net version 4.5 so please suggest me how to create a place holder in string variable and put my value there without using Replace function. i try this approach too but still no luck.
private void button1_Click(object sender, EventArgs e)
{
string strBMFormula = Replacement("A10", "Z210");
}private string Replacement(string value1, string value2)
{
return @"IF(AND(ISNUMBER({value1}),{value2}>0,NOT(ISERROR({value1}/{value2}))),{value1}/{value2}-1,"""")";
}I got fix for my above code. here it is.
private void button1_Click(object sender, EventArgs e)
{
string strBMFormula = Replacement("A10", "Z210");
}private string Replacement(string value1, string value2)
{
return string.Format(@"IF(AND(ISNUMBER({0}),{1}>0,NOT(ISERROR({0}/{1}))),{0}/{1}-1,"""")",value1,value2);
} -
see the code how i am creating place holder now. _Yoy has a formula used in excel.
private string _Yoy = @"IF(AND(ISNUMBER(C#),P#>0,NOT(ISERROR(C#/P#))),C#/P#-1,"""")";
public string YoY
{
get { return _Yoy; }
set { _Yoy = value; }
}private void button1\_Click(object sender, EventArgs e) { string strBMFormula = YoY.Replace("P#", "2005.13").Replace("C#", "7777.10"); }
I am replacing P# & C# with some value at runtime and program working as expected but i am doing this in large loop where Replace() function is getting called repeatedly which may increase memory use.
@"IF(AND(ISNUMBER(C#),P#>0,NOT(ISERROR(C#/P#))),C#/P#-1,"""")";
here i gave 4 double quote because there would two double quote in excel function. i am using .Net version 4.5 so please suggest me how to create a place holder in string variable and put my value there without using Replace function. i try this approach too but still no luck.
private void button1_Click(object sender, EventArgs e)
{
string strBMFormula = Replacement("A10", "Z210");
}private string Replacement(string value1, string value2)
{
return @"IF(AND(ISNUMBER({value1}),{value2}>0,NOT(ISERROR({value1}/{value2}))),{value1}/{value2}-1,"""")";
}You are confused with string modifiers. You are using the verbatim string modifier (@), which allows you to avoid having to escape special characters (no double-antislash, for example). But you need the interpolated-string modifier,
$
.private string Replacement(string value1, string value2)
{
return $"IF(AND(ISNUMBER({value1}),{value2}>0,NOT(ISERROR({value1}/{value2}))),{value1}/{value2}-1,"""")";
}"Five fruits and vegetables a day? What a joke! Personally, after the third watermelon, I'm full."
-
You are confused with string modifiers. You are using the verbatim string modifier (@), which allows you to avoid having to escape special characters (no double-antislash, for example). But you need the interpolated-string modifier,
$
.private string Replacement(string value1, string value2)
{
return $"IF(AND(ISNUMBER({value1}),{value2}>0,NOT(ISERROR({value1}/{value2}))),{value1}/{value2}-1,"""")";
}"Five fruits and vegetables a day? What a joke! Personally, after the third watermelon, I'm full."
-
see the code how i am creating place holder now. _Yoy has a formula used in excel.
private string _Yoy = @"IF(AND(ISNUMBER(C#),P#>0,NOT(ISERROR(C#/P#))),C#/P#-1,"""")";
public string YoY
{
get { return _Yoy; }
set { _Yoy = value; }
}private void button1\_Click(object sender, EventArgs e) { string strBMFormula = YoY.Replace("P#", "2005.13").Replace("C#", "7777.10"); }
I am replacing P# & C# with some value at runtime and program working as expected but i am doing this in large loop where Replace() function is getting called repeatedly which may increase memory use.
@"IF(AND(ISNUMBER(C#),P#>0,NOT(ISERROR(C#/P#))),C#/P#-1,"""")";
here i gave 4 double quote because there would two double quote in excel function. i am using .Net version 4.5 so please suggest me how to create a place holder in string variable and put my value there without using Replace function. i try this approach too but still no luck.
private void button1_Click(object sender, EventArgs e)
{
string strBMFormula = Replacement("A10", "Z210");
}private string Replacement(string value1, string value2)
{
return @"IF(AND(ISNUMBER({value1}),{value2}>0,NOT(ISERROR({value1}/{value2}))),{value1}/{value2}-1,"""")";
}First, what's stopping you from using later/latest versions of VS and FrameWork/C# ? Given those are free, why not upgrade ? Try this:
const string YTemplate = @"IF(AND(ISNUMBER(C#),P#>0,NOT(ISERROR(C#/P#))),C#/P#-1,"""")";
StringBuilder sb = new StringBuilder(YTemplate);// in a method:
sb = sb.Replace("P#", "2005.13").Replace("C#", "7777.10");To reuse this, clear the StringBuilder after a use, and then reset its contents to 'YTemplate. Faster, more efficient ? Lots of different opinions on String.Replace and StringBuilder.Replace: [^] Do some tests with your data, and find out if it's better for your case. And, keep your eye on the future: [^]
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
-
First, what's stopping you from using later/latest versions of VS and FrameWork/C# ? Given those are free, why not upgrade ? Try this:
const string YTemplate = @"IF(AND(ISNUMBER(C#),P#>0,NOT(ISERROR(C#/P#))),C#/P#-1,"""")";
StringBuilder sb = new StringBuilder(YTemplate);// in a method:
sb = sb.Replace("P#", "2005.13").Replace("C#", "7777.10");To reuse this, clear the StringBuilder after a use, and then reset its contents to 'YTemplate. Faster, more efficient ? Lots of different opinions on String.Replace and StringBuilder.Replace: [^] Do some tests with your data, and find out if it's better for your case. And, keep your eye on the future: [^]
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
I am doing this way and it works fine.
public static class StringHelpers
{
public static string BuildFormula(this string formulatemplate, string value1, string value2)
{
return string.Format(formulatemplate, value1, value2);
}public static string BuildFormula(this string formulatemplate, string value1, string value2, string value3) { return string.Format(formulatemplate, value1, value2, value3); } }
Calling this way ---------------------
BrokerCountFormula = FormulaTemplate.BrokerCount.BuildFormula(
(StartPeriod + row.ToString()), (EndPeriod + row.ToString())); -
First, what's stopping you from using later/latest versions of VS and FrameWork/C# ? Given those are free, why not upgrade ? Try this:
const string YTemplate = @"IF(AND(ISNUMBER(C#),P#>0,NOT(ISERROR(C#/P#))),C#/P#-1,"""")";
StringBuilder sb = new StringBuilder(YTemplate);// in a method:
sb = sb.Replace("P#", "2005.13").Replace("C#", "7777.10");To reuse this, clear the StringBuilder after a use, and then reset its contents to 'YTemplate. Faster, more efficient ? Lots of different opinions on String.Replace and StringBuilder.Replace: [^] Do some tests with your data, and find out if it's better for your case. And, keep your eye on the future: [^]
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
I am doing this way and it works fine.
public static class StringHelpers
{
public static string BuildFormula(this string formulatemplate, string value1, string value2)
{
return string.Format(formulatemplate, value1, value2);
}public static string BuildFormula(this string formulatemplate, string value1, string value2, string value3) { return string.Format(formulatemplate, value1, value2, value3); } }
Calling this way ---------------------
BrokerCountFormula = FormulaTemplate.BrokerCount.BuildFormula(
(StartPeriod + row.ToString()), (EndPeriod + row.ToString()));