Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# How to use place holder in string variable

C# How to use place holder in string variable

Scheduled Pinned Locked Moved C#
csharpperformancetutorialannouncement
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mou_kol
    wrote on last edited by
    #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,"""")";
    }

    M P B 3 Replies Last reply
    0
    • M Mou_kol

      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,"""")";
      }

      M Offline
      M Offline
      Mou_kol
      wrote on last edited by
      #2

      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);
      }

      1 Reply Last reply
      0
      • M Mou_kol

        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,"""")";
        }

        P Offline
        P Offline
        phil o
        wrote on last edited by
        #3

        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."

        M 1 Reply Last reply
        0
        • P phil o

          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."

          M Offline
          M Offline
          Mou_kol
          wrote on last edited by
          #4

          string interpolation not available in my .net version. i am using c# v5.0 & VS2013.

          1 Reply Last reply
          0
          • M Mou_kol

            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,"""")";
            }

            B Offline
            B Offline
            BillWoodruff
            wrote on last edited by
            #5

            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

            M 2 Replies Last reply
            0
            • B BillWoodruff

              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

              M Offline
              M Offline
              Mou_kol
              wrote on last edited by
              #6

              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()));

              1 Reply Last reply
              0
              • B BillWoodruff

                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

                M Offline
                M Offline
                Mou_kol
                wrote on last edited by
                #7

                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()));

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups