locked
Single quotes in c# RRS feed

  • Question

  • User52625461 posted

    Hello All,

    I have the following string in french languages which contains single quotes between the characters and words. This message I want to show in alert. But due to single quotes it is giving me error as missing ) after argument list. Even I have tried to add single quote at start and end of the string

    Below is my code

    string msg = ATS.Web.Core.Classes.ResourceManager.GetLocalString("Admin_JobDetail.ascx", "JobSavedMsg");
    msg = "\'\"" + msg + "*\"\'";
    Page.Response.Write("<script>alert('" + msg + "');location.href='" + ATS.Web.Core.Admin.SessionBag.Organization.ReferalPage + "'</script>");

    In msg variable I am getting the french string which is as follows

    L'emploi de l'organisation a été créé, mais la configuration n'est pas terminée.Pour terminer la configuration, veuillez configurer le workflow, ajouter les groupes d'utilisateurs et toute autre information importante sous les onglets correspondants.

    Any help on this appreciated !

    Wednesday, January 20, 2016 9:34 AM

Answers

  • User-434868552 posted

    @rohitpundlik

    Let's look at your code, simplified for testing, in small steps:

    // Home/ASP.NET Forums/.NET Languages/C#/Single quotes in c#
    String msg ="L'emploi de l'organisation a été créé, mais la configuration n'est pas terminée.Pour terminer la configuration, veuillez configurer le workflow, ajouter les groupes d'utilisateurs et toute autre information importante sous les onglets correspondants.";
    Console.WriteLine(msg);

    output:

    L'emploi de l'organisation a été créé, mais la configuration n'est pas terminée.Pour terminer la configuration, veuillez configurer le workflow, ajouter les groupes d'utilisateurs et toute autre information importante sous les onglets correspondants.

    next:

    msg = "\'\"" + msg + "*\"\'"; // this line is likely the cause of your grief
    Console.WriteLine(msg);

    output ... NOTE:    msg  is now already surrounded by single quotes:

    '"L'emploi de l'organisation a été créé, mais la configuration n'est pas terminée.Pour terminer la configuration, veuillez configurer le workflow, ajouter les groupes d'utilisateurs et toute autre information importante sous les onglets correspondants.*"'

    finally:

    String referalPage = "whatever"; // fake String value for testing
    Console.WriteLine("<script>alert('" + msg + "');location.href='" + referalPage + "'</script>");

    output:      note that your emitted JavaScript begins:        alert(''

    <script>alert(''"L'emploi de l'organisation a été créé, mais la configuration n'est pas terminée.Pour terminer la configuration, veuillez configurer le workflow, ajouter les groupes d'utilisateurs et toute autre information importante sous les onglets correspondants.*"'');location.href='whatever'</script>

    FIXING YOUR CODE

    drop this line:

    msg = "\'\"" + msg + "*\"\'";

    output (not the final solution):

    <script>alert('L'emploi de l'organisation a été créé, mais la configuration n'est pas terminée.Pour terminer la configuration, veuillez configurer le workflow, ajouter les groupes d'utilisateurs et toute autre information importante sous les onglets correspondants.');location.href='whatever'</script>

    MORE  INFORMATION

    SYNTAX:        JavaScript      alert

    (a) surrounded by DOUBLE quotes:    alert("text");

    (b) surrounded by SINGLE quotes:     alert('text');

    CHALLENGE:       to have properly paired quotes.        [ see edits below, HttpUtility.JavaScriptStringEncode Method is better than String Replace ]

    solution (a) if you are using surrounding DOUBLE quotes, add this line: 

    msg = msg.Replace("\"", "\"\"");
    msg = msg.Replace("\"", @"\""");

    solution (b) if you are using surrounding SINGLE quotes, add this line: 

    msg = msg.Replace("\'", @"\u0027");

    Here's the new version of YOUR code since you are using surround SINGLE quotes:

    String msg = "L'emploi de l'organisation a été créé, mais la configuration n'est pas terminée.Pour terminer la configuration, veuillez configurer le workflow, ajouter les groupes d'utilisateurs et toute autre information importante sous les onglets correspondants.";
    msg = msg.Replace("\'", "\'\'");  *** WRONG ***
    msg = msg.Replace("\'", @"\u0027"); String referalPage = "whatever"; // fake String value for testing Console.WriteLine("<script>alert('" + msg + "');location.href='" + referalPage + "'</script>");

    output:

    <script>alert('L''emploi de l''organisation a été créé, mais la configuration n''est pas terminée.Pour terminer la configuration, veuillez configurer le workflow, ajouter les groupes d''utilisateurs et toute autre information importante sous les onglets correspondants.');location.href='whatever'</script>

     

    <script>alert('L\u0027emploi de l\u0027organisation a été créé, mais la configuration n\u0027est pas terminée.Pour terminer la configuration, veuillez configurer le workflow, ajouter les groupes d\u0027utilisateurs et toute autre information importante sous les onglets correspondants.');location.href='whatever'</script>

     

    EDIT:

    see http://forums.asp.net/post/6012309.aspx below by PatriceSc:

    https://msdn.microsoft.com/en-us/library/system.web.httputility.javascriptstringencode(v=vs.110).aspx 
       "HttpUtility.JavaScriptStringEncode Method"

    The JavaScriptStringEncode Method is a better than String Replace because it deals with multiple issues

    String msg = "L'emploi de l'organisation a été créé, mais la configuration n'est pas terminée.Pour terminer la configuration, veuillez configurer le workflow, ajouter les groupes d'utilisateurs et toute autre information importante sous les onglets correspondants.";
    msg = System.Web.HttpUtility.JavaScriptStringEncode(msg);
    String referalPage = "whatever"; // fake String value for testing
    Console.WriteLine("<script>alert('" + msg + "');location.href='" + referalPage + "'</script>");

    output:

    <script>alert('L\u0027emploi de l\u0027organisation a été créé, mais la configuration n\u0027est pas terminée.Pour terminer la configuration, veuillez configurer le workflow, ajouter les groupes d\u0027utilisateurs et toute autre information importante sous les onglets correspondants.');location.href='whatever'</script>
    

    END EDIT.

    EDIT #2:

    http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf "ECMAScript® 2015 Language Specification"

    see section  11.8.4   String Literals

                also 11.8.4.3:          SV =. string value

    The SV of StringLiteral :: "" is the empty code unit sequence.
    The SV of StringLiteral :: '' is the empty code unit sequence.

    see also:  http://www.w3schools.com/js/js_strings.asp

    BOTTOM LINE:  while c# is happy with strings like:   

    String twoDoubleQuotes = @"""""";
    Console.WriteLine(twoDoubleQuotes);

    output:

    ""

    JavaScript expects escape characters instead:

    String twoDoubleQuotes = @""""""; // okay c# string
    // convert for emitted JavaScript:
    twoDoubleQuotes = System.Web.HttpUtility.JavaScriptStringEncode(twoDoubleQuotes);
    Console.WriteLine(twoDoubleQuotes);
    

    output to make JavaScript happy:

    \"\"

    END EDIT #2.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 20, 2016 11:37 AM
  • User753101303 posted

    It would be :

    msg = System.Web.HttpUtility.JavaScriptStringEncode(msg);

    And then use msg as you are doing currently. As shown by others it will encode escape the ' character but should take care as well of any other character that could require being escaped or maybe encoded.

    Else the single quote is taken as the end of the JavaScript literal string and then you have a syntax error.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 20, 2016 11:46 AM

All replies

  • User753101303 posted

    Hi,

    See https://msdn.microsoft.com/en-us/library/dd991914(v=vs.110).aspx to encode strings used in JavaScript code (it will encode the ' character that is taken otherwise as closing a literal string, causing a syntax error when trying to parse the rest).

    Or is this really a C# side error?

    Wednesday, January 20, 2016 9:47 AM
  • User52625461 posted

    Hi,

    See https://msdn.microsoft.com/en-us/library/dd991914(v=vs.110).aspx to encode strings used in JavaScript code (it will encode the ' character that is taken otherwise as closing a literal string, causing a syntax error when trying to parse the rest).

    Or is this really a C# side error?

    No its a JS Error as it is shown on browser window. To avoid this how can I pass correct string from c# to JS ?

    Wednesday, January 20, 2016 9:59 AM
  • User-698989805 posted

    I am not sure if this works for you. But I saw this working to one of my friend. Please try this:

    ToString.Replace("'", "\\'")

    Wednesday, January 20, 2016 10:07 AM
  • User-1716253493 posted

    Maybe you can replace single quote in msg with @"\'" first

    L\'emploi de l\'organisation a été créé, mais la configuration n\'est pas 

    You have already done in C# (escaping), but not yet in Java, you need also do that in Java

    msg = @"L\'emploi de l\'organisation a été créé, mais la configuration n\'est pas ";

    Wednesday, January 20, 2016 10:08 AM
  • User-434868552 posted

    @rohitpundlik

    Let's look at your code, simplified for testing, in small steps:

    // Home/ASP.NET Forums/.NET Languages/C#/Single quotes in c#
    String msg ="L'emploi de l'organisation a été créé, mais la configuration n'est pas terminée.Pour terminer la configuration, veuillez configurer le workflow, ajouter les groupes d'utilisateurs et toute autre information importante sous les onglets correspondants.";
    Console.WriteLine(msg);

    output:

    L'emploi de l'organisation a été créé, mais la configuration n'est pas terminée.Pour terminer la configuration, veuillez configurer le workflow, ajouter les groupes d'utilisateurs et toute autre information importante sous les onglets correspondants.

    next:

    msg = "\'\"" + msg + "*\"\'"; // this line is likely the cause of your grief
    Console.WriteLine(msg);

    output ... NOTE:    msg  is now already surrounded by single quotes:

    '"L'emploi de l'organisation a été créé, mais la configuration n'est pas terminée.Pour terminer la configuration, veuillez configurer le workflow, ajouter les groupes d'utilisateurs et toute autre information importante sous les onglets correspondants.*"'

    finally:

    String referalPage = "whatever"; // fake String value for testing
    Console.WriteLine("<script>alert('" + msg + "');location.href='" + referalPage + "'</script>");

    output:      note that your emitted JavaScript begins:        alert(''

    <script>alert(''"L'emploi de l'organisation a été créé, mais la configuration n'est pas terminée.Pour terminer la configuration, veuillez configurer le workflow, ajouter les groupes d'utilisateurs et toute autre information importante sous les onglets correspondants.*"'');location.href='whatever'</script>

    FIXING YOUR CODE

    drop this line:

    msg = "\'\"" + msg + "*\"\'";

    output (not the final solution):

    <script>alert('L'emploi de l'organisation a été créé, mais la configuration n'est pas terminée.Pour terminer la configuration, veuillez configurer le workflow, ajouter les groupes d'utilisateurs et toute autre information importante sous les onglets correspondants.');location.href='whatever'</script>

    MORE  INFORMATION

    SYNTAX:        JavaScript      alert

    (a) surrounded by DOUBLE quotes:    alert("text");

    (b) surrounded by SINGLE quotes:     alert('text');

    CHALLENGE:       to have properly paired quotes.        [ see edits below, HttpUtility.JavaScriptStringEncode Method is better than String Replace ]

    solution (a) if you are using surrounding DOUBLE quotes, add this line: 

    msg = msg.Replace("\"", "\"\"");
    msg = msg.Replace("\"", @"\""");

    solution (b) if you are using surrounding SINGLE quotes, add this line: 

    msg = msg.Replace("\'", @"\u0027");

    Here's the new version of YOUR code since you are using surround SINGLE quotes:

    String msg = "L'emploi de l'organisation a été créé, mais la configuration n'est pas terminée.Pour terminer la configuration, veuillez configurer le workflow, ajouter les groupes d'utilisateurs et toute autre information importante sous les onglets correspondants.";
    msg = msg.Replace("\'", "\'\'");  *** WRONG ***
    msg = msg.Replace("\'", @"\u0027"); String referalPage = "whatever"; // fake String value for testing Console.WriteLine("<script>alert('" + msg + "');location.href='" + referalPage + "'</script>");

    output:

    <script>alert('L''emploi de l''organisation a été créé, mais la configuration n''est pas terminée.Pour terminer la configuration, veuillez configurer le workflow, ajouter les groupes d''utilisateurs et toute autre information importante sous les onglets correspondants.');location.href='whatever'</script>

     

    <script>alert('L\u0027emploi de l\u0027organisation a été créé, mais la configuration n\u0027est pas terminée.Pour terminer la configuration, veuillez configurer le workflow, ajouter les groupes d\u0027utilisateurs et toute autre information importante sous les onglets correspondants.');location.href='whatever'</script>

     

    EDIT:

    see http://forums.asp.net/post/6012309.aspx below by PatriceSc:

    https://msdn.microsoft.com/en-us/library/system.web.httputility.javascriptstringencode(v=vs.110).aspx 
       "HttpUtility.JavaScriptStringEncode Method"

    The JavaScriptStringEncode Method is a better than String Replace because it deals with multiple issues

    String msg = "L'emploi de l'organisation a été créé, mais la configuration n'est pas terminée.Pour terminer la configuration, veuillez configurer le workflow, ajouter les groupes d'utilisateurs et toute autre information importante sous les onglets correspondants.";
    msg = System.Web.HttpUtility.JavaScriptStringEncode(msg);
    String referalPage = "whatever"; // fake String value for testing
    Console.WriteLine("<script>alert('" + msg + "');location.href='" + referalPage + "'</script>");

    output:

    <script>alert('L\u0027emploi de l\u0027organisation a été créé, mais la configuration n\u0027est pas terminée.Pour terminer la configuration, veuillez configurer le workflow, ajouter les groupes d\u0027utilisateurs et toute autre information importante sous les onglets correspondants.');location.href='whatever'</script>
    

    END EDIT.

    EDIT #2:

    http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf "ECMAScript® 2015 Language Specification"

    see section  11.8.4   String Literals

                also 11.8.4.3:          SV =. string value

    The SV of StringLiteral :: "" is the empty code unit sequence.
    The SV of StringLiteral :: '' is the empty code unit sequence.

    see also:  http://www.w3schools.com/js/js_strings.asp

    BOTTOM LINE:  while c# is happy with strings like:   

    String twoDoubleQuotes = @"""""";
    Console.WriteLine(twoDoubleQuotes);

    output:

    ""

    JavaScript expects escape characters instead:

    String twoDoubleQuotes = @""""""; // okay c# string
    // convert for emitted JavaScript:
    twoDoubleQuotes = System.Web.HttpUtility.JavaScriptStringEncode(twoDoubleQuotes);
    Console.WriteLine(twoDoubleQuotes);
    

    output to make JavaScript happy:

    \"\"

    END EDIT #2.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 20, 2016 11:37 AM
  • User753101303 posted

    It would be :

    msg = System.Web.HttpUtility.JavaScriptStringEncode(msg);

    And then use msg as you are doing currently. As shown by others it will encode escape the ' character but should take care as well of any other character that could require being escaped or maybe encoded.

    Else the single quote is taken as the end of the JavaScript literal string and then you have a syntax error.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 20, 2016 11:46 AM
  • User52625461 posted

    Hello PatriceSc and gerrylowry

    Thanks for the help. The things worked like a charm !

    Thursday, January 21, 2016 12:03 PM