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. Using double quotes in a string

Using double quotes in a string

Scheduled Pinned Locked Moved C#
csharpjavajavascriptasp-netvisual-studio
7 Posts 6 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.
  • C Offline
    C Offline
    compninja25
    wrote on last edited by
    #1

    Hey all. I know I've done this in the past and I'm not having much luck searching online because it should...just work. I'm trying to save some javascript in a code behind and then calling it in the ASP.NET page using <%= Java%>. It's a simple time ticker that I want to reset all the time to countdown to Friday. My problem now is the string isn't accepting the \ as the escape for my double quotes.

    public string Java
    {
    get
    {
    string java = ";";
    java += "TargetDate =\" " + friday + "\"";
    java += "BackColor = \"palegreen\";";
    java += "ForeColor = \"navy\"; ";
    java += "CountActive = true;";
    java += "CountStepper = -1;";
    java += "LeadingZero = true;";
    java += "DisplayFormat = \"%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.\";";
    java += "FinishMessage = \"It is finally FRIDAY!\";";
    return java;
    }
    }

    * There might be a few extra "'s here and there as a result of copying and pasting out of VS * every time I run the debugger, the string reads ect.... What's really weird is if I replace the double quote with a single quote, then the string acts the way I want it to, but not with the double quotes! (<script language="JavaScript"> ect...) Has anyone ever seen this problem before? Is it possible something got screwed up with my VS 2008 install? <div class="ForumSig">"You're damned if you do, and you're damned if you dont" - Bart Simpson</div>

    J D C 3 Replies Last reply
    0
    • C compninja25

      Hey all. I know I've done this in the past and I'm not having much luck searching online because it should...just work. I'm trying to save some javascript in a code behind and then calling it in the ASP.NET page using <%= Java%>. It's a simple time ticker that I want to reset all the time to countdown to Friday. My problem now is the string isn't accepting the \ as the escape for my double quotes.

      public string Java
      {
      get
      {
      string java = ";";
      java += "TargetDate =\" " + friday + "\"";
      java += "BackColor = \"palegreen\";";
      java += "ForeColor = \"navy\"; ";
      java += "CountActive = true;";
      java += "CountStepper = -1;";
      java += "LeadingZero = true;";
      java += "DisplayFormat = \"%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.\";";
      java += "FinishMessage = \"It is finally FRIDAY!\";";
      return java;
      }
      }

      * There might be a few extra "'s here and there as a result of copying and pasting out of VS * every time I run the debugger, the string reads ect.... What's really weird is if I replace the double quote with a single quote, then the string acts the way I want it to, but not with the double quotes! (<script language="JavaScript"> ect...) Has anyone ever seen this problem before? Is it possible something got screwed up with my VS 2008 install? <div class="ForumSig">"You're damned if you do, and you're damned if you dont" - Bart Simpson</div>

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      The debugger will always show the escape char '\'. Dont worry, it wont be in the output. A few comments on your code: 1) Its Javascript. Java is a very different beast 2) Consider using a StringBuilder rather than lots of string concatenation using += 3) Look at

      Page.ClientScript.RegisterClientScriptBlock

      . Its preferable to simply trying to output some script to the page using the old-asp method of <%= %>

      V 1 Reply Last reply
      0
      • C compninja25

        Hey all. I know I've done this in the past and I'm not having much luck searching online because it should...just work. I'm trying to save some javascript in a code behind and then calling it in the ASP.NET page using <%= Java%>. It's a simple time ticker that I want to reset all the time to countdown to Friday. My problem now is the string isn't accepting the \ as the escape for my double quotes.

        public string Java
        {
        get
        {
        string java = ";";
        java += "TargetDate =\" " + friday + "\"";
        java += "BackColor = \"palegreen\";";
        java += "ForeColor = \"navy\"; ";
        java += "CountActive = true;";
        java += "CountStepper = -1;";
        java += "LeadingZero = true;";
        java += "DisplayFormat = \"%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.\";";
        java += "FinishMessage = \"It is finally FRIDAY!\";";
        return java;
        }
        }

        * There might be a few extra "'s here and there as a result of copying and pasting out of VS * every time I run the debugger, the string reads ect.... What's really weird is if I replace the double quote with a single quote, then the string acts the way I want it to, but not with the double quotes! (<script language="JavaScript"> ect...) Has anyone ever seen this problem before? Is it possible something got screwed up with my VS 2008 install? <div class="ForumSig">"You're damned if you do, and you're damned if you dont" - Bart Simpson</div>

        D Offline
        D Offline
        Deresen
        wrote on last edited by
        #3

        As J4amieC says, you can use the Stringbuilder. But you can also use String.Format(); Very easy to use:

        String javaScript = String.Format(@"
        etc.
        {0} <- a variable
        {1} <- another variable", variable1, variable2);

        G 1 Reply Last reply
        0
        • D Deresen

          As J4amieC says, you can use the Stringbuilder. But you can also use String.Format(); Very easy to use:

          String javaScript = String.Format(@"
          etc.
          {0} <- a variable
          {1} <- another variable", variable1, variable2);

          G Offline
          G Offline
          Giorgi Dalakishvili
          wrote on last edited by
          #4

          String.Format() uses StringBuilder internally.

          Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion

          1 Reply Last reply
          0
          • C compninja25

            Hey all. I know I've done this in the past and I'm not having much luck searching online because it should...just work. I'm trying to save some javascript in a code behind and then calling it in the ASP.NET page using <%= Java%>. It's a simple time ticker that I want to reset all the time to countdown to Friday. My problem now is the string isn't accepting the \ as the escape for my double quotes.

            public string Java
            {
            get
            {
            string java = ";";
            java += "TargetDate =\" " + friday + "\"";
            java += "BackColor = \"palegreen\";";
            java += "ForeColor = \"navy\"; ";
            java += "CountActive = true;";
            java += "CountStepper = -1;";
            java += "LeadingZero = true;";
            java += "DisplayFormat = \"%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.\";";
            java += "FinishMessage = \"It is finally FRIDAY!\";";
            return java;
            }
            }

            * There might be a few extra "'s here and there as a result of copying and pasting out of VS * every time I run the debugger, the string reads ect.... What's really weird is if I replace the double quote with a single quote, then the string acts the way I want it to, but not with the double quotes! (<script language="JavaScript"> ect...) Has anyone ever seen this problem before? Is it possible something got screwed up with my VS 2008 install? <div class="ForumSig">"You're damned if you do, and you're damned if you dont" - Bart Simpson</div>

            C Offline
            C Offline
            cor2879
            wrote on last edited by
            #5

            I do a lot of dynamically created JavaScript in my ASP.Net development. I use a pattern similar to this:

            protected void Page_PreRender(object sender, EventArgs e)
            {
            /* Call your method that registers JavaScript from here. This event is called even if
            * you only have a Call Back rather than a Post Back
            */

             RegisterClientScript();
            

            }

            // This method contains any dynamic JavaScript that I wish to register on the page
            protected void RegisterClientScript()
            {
            // Check first to see if the page has already had the Javascript code registered.
            if (!(Page.ClientScript.IsStartupScriptRegistered("myJS"))
            {
            // Use a string builder to, well, build the string. This limits the number of immutable strings that are added to memory and must
            // be eventually garbage collected.
            StringBuilder js = new StringBuilder();

            	// Build the JavaScript code.  Note that inside the string we are actually using single quotes - this is legal in JavaScript for strings. 
            	// In this way you don't even have to mess with escape characters.  At least not as often - there may still be occasions where you will
            	// need single quotes.
            	js.AppendLine("TargetDate = 'friday';");
            	js.AppendLine("BackColor = 'palegreen';");
            	js.AppendLine("ForeColor = 'navy';");
            	js.AppendLine("CountActive = true;");
            	js.AppendLine("CountStepper = -1;");
            	js.AppendLine("LeadingZero = true;");
            	js.AppendLine("DisplayFormat = '%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.';");
            	js.AppendLine("FinishMessage = 'It is finally FRIDAY!'");
            	
            	/\* Call the register startup script method.  This will call the above script to execute when the page renders.  The first parameter is any instance of the
            	  \* System.Type class, but usually you want to pass the type of this Page or Control.  Next is the key, then the JavaScript string itself.  The last boolean value
            	  \* specifies whether or not script tags should be added to the JavaScript code.  You can add them yourself or just do as I did and have the RegisterClientScript method
            	  \* do this for you.  To me this way is cleaner.
            	  \*/
            	Page.ClientScript.RegisterStartupScript(this.GetType(), "myJS", js.ToString(), true);
            }
            

            }

            "We are men of action; lies do not become us."

            C 1 Reply Last reply
            0
            • C cor2879

              I do a lot of dynamically created JavaScript in my ASP.Net development. I use a pattern similar to this:

              protected void Page_PreRender(object sender, EventArgs e)
              {
              /* Call your method that registers JavaScript from here. This event is called even if
              * you only have a Call Back rather than a Post Back
              */

               RegisterClientScript();
              

              }

              // This method contains any dynamic JavaScript that I wish to register on the page
              protected void RegisterClientScript()
              {
              // Check first to see if the page has already had the Javascript code registered.
              if (!(Page.ClientScript.IsStartupScriptRegistered("myJS"))
              {
              // Use a string builder to, well, build the string. This limits the number of immutable strings that are added to memory and must
              // be eventually garbage collected.
              StringBuilder js = new StringBuilder();

              	// Build the JavaScript code.  Note that inside the string we are actually using single quotes - this is legal in JavaScript for strings. 
              	// In this way you don't even have to mess with escape characters.  At least not as often - there may still be occasions where you will
              	// need single quotes.
              	js.AppendLine("TargetDate = 'friday';");
              	js.AppendLine("BackColor = 'palegreen';");
              	js.AppendLine("ForeColor = 'navy';");
              	js.AppendLine("CountActive = true;");
              	js.AppendLine("CountStepper = -1;");
              	js.AppendLine("LeadingZero = true;");
              	js.AppendLine("DisplayFormat = '%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.';");
              	js.AppendLine("FinishMessage = 'It is finally FRIDAY!'");
              	
              	/\* Call the register startup script method.  This will call the above script to execute when the page renders.  The first parameter is any instance of the
              	  \* System.Type class, but usually you want to pass the type of this Page or Control.  Next is the key, then the JavaScript string itself.  The last boolean value
              	  \* specifies whether or not script tags should be added to the JavaScript code.  You can add them yourself or just do as I did and have the RegisterClientScript method
              	  \* do this for you.  To me this way is cleaner.
              	  \*/
              	Page.ClientScript.RegisterStartupScript(this.GetType(), "myJS", js.ToString(), true);
              }
              

              }

              "We are men of action; lies do not become us."

              C Offline
              C Offline
              compninja25
              wrote on last edited by
              #6

              Thank you everyone for the replies. I apologize, I'm not as familiar with Javascript yet, which is why up until now I would do more logic and coding in the C# behind pages and then just call the strings as needed. I've setup my page behind using the code above and it appears to insert the code properly, however it's still not working correctly. What I am trying to do is have the "TargetDate dynamically change to that week's upcoming friday. If I goto my asp page and put this in, it works just fine:

              <script language="JavaScript">
              TargetDate = '12/12/2008 12:00:00 AM';
              BackColor = 'palegreen';
              ForeColor = 'navy';
              CountActive = true;
              CountStepper = -1;
              LeadingZero = true;
              DisplayFormat = '%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.';
              FinishMessage = 'It is finally FRIDAY!';
              </script>
              <script language="JavaScript" src="countdown.js"></script>

              I then rip out the first script part and use the outlined code above. This will start the countdown timer but it can't find any of the settings I have in place. If I do a view source of the page, at the very bottom of the page I now see this:

              <script language="JavaScript">
              TargetDate = '12/12/2008 12:00:00 AM';
              BackColor = 'palegreen';
              ForeColor = 'navy';
              CountActive = true;
              CountStepper = -1;
              LeadingZero = true;
              DisplayFormat = '%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.';
              FinishMessage = 'It is finally FRIDAY!';
              </script>

              I'm starting to wonder now if it makes more sense to just modify the "countdown.js" and put the switch statements in there to always update the date. Again, thank you for all of your help!

              "You're damned if you do, and you're damned if you dont" - Bart Simpson

              1 Reply Last reply
              0
              • J J4amieC

                The debugger will always show the escape char '\'. Dont worry, it wont be in the output. A few comments on your code: 1) Its Javascript. Java is a very different beast 2) Consider using a StringBuilder rather than lots of string concatenation using += 3) Look at

                Page.ClientScript.RegisterClientScriptBlock

                . Its preferable to simply trying to output some script to the page using the old-asp method of <%= %>

                V Offline
                V Offline
                Vikram A Punathambekar
                wrote on last edited by
                #7

                J4amieC wrote:

                Consider using a StringBuilder rather than lots of string concatenation using +=

                If the strings are known at compile time, the compiler will generate one big string for you. Try compiling this and taking apart the EXE with ildasm and you will see what I mean.

                using System;

                class Foo
                {
                public static int Main(string[] args)
                {
                string s = "Hello, " + "world!";
                Console.WriteLine(s);

                	return 0;
                }
                

                }

                Cheers, Vıkram.


                Stand up to be seen. Speak up to be heard. Shut up to be appreciated.

                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