473,382 Members | 1,365 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

string with quotes

27
Hi,
Below is my javascript string which i will be using in asp.net

Expand|Select|Wrap|Line Numbers
  1.  sTemp = "<script language=javascript>;parent.document.getElementById('ctl00$ContentPlaceHolder1$txt_name').value ='" + myname + " ' ;</script>"
  2. ClientScript.RegisterStartupScript(Me.GetType, "myscript", sTemp)

variable myname is having single quotes values (myname =John O'Loughlin
) .I get javascript error (Expected ';') when i register the script

any solution to assign value with variables having single quotes?

Thanks
Mpl
Jul 30 '08 #1
1 1446
Hello,

This is more of a .NET question... To paste the contents of the variable you should escape the quotes with a backslash. Replace every quote with backslash+quote.
But keep in mind, this is still dangerous so please check this http://www.google.com/search?hl=en&q...ng&btnG=Search The first link it returns is http://www.west-wind.com/weblog/posts/114530.aspx which I've found to be very helpful for what you need.
Expand|Select|Wrap|Line Numbers
  1. /// <summary>
  2. /// Encodes a string to be represented as a string literal. The format
  3. /// is essentially a JSON string.
  4. /// 
  5. /// The string returned includes outer quotes 
  6. /// Example Output: "Hello \"Rick\"!\r\nRock on"
  7. /// </summary>
  8. /// <param name="s"></param>
  9. /// <returns></returns>
  10. public static string EncodeJsString(string s)
  11. {
  12.     StringBuilder sb = new StringBuilder();
  13.     sb.Append("\"");
  14.     foreach (char c in s)
  15.     {
  16.         switch (c)
  17.         {
  18.             case '\"':
  19.                 sb.Append("\\\"");
  20.                 break;
  21.             case '\\':
  22.                 sb.Append("\\\\");
  23.                 break;
  24.             case '\b':
  25.                 sb.Append("\\b");
  26.                 break;
  27.             case '\f':
  28.                 sb.Append("\\f");
  29.                 break;
  30.             case '\n':
  31.                 sb.Append("\\n");
  32.                 break;
  33.             case '\r':
  34.                 sb.Append("\\r");
  35.                 break;
  36.             case '\t':
  37.                 sb.Append("\\t");
  38.                 break;
  39.             default:
  40.                 int i = (int)c;
  41.                 if (i < 32 || i > 127)
  42.                 {
  43.                     sb.AppendFormat("\\u{0:X04}", i);
  44.                 }
  45.                 else
  46.                 {
  47.                     sb.Append(c);
  48.                 }
  49.                 break;
  50.         }
  51.     }
  52.     sb.Append("\"");
  53.  
  54.     return sb.ToString();
  55. }
Regards,
Tom
Jul 30 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Bengt Richter | last post by:
Why wouldn't quote-stuffing solve the problem, and let you treat \ as an ordinary character? In a raw string, it's no good for preventing end-of-quoting anyway, unless you want the literal \ in...
9
by: PaulThomas | last post by:
I want to "look" through a comma delimited String and "take it apart" by finding the comma's and then put each "set of characters" into seperate strings - - - maybe an array, maybe seperate cells...
5
by: Ann Marinas | last post by:
Happy New Year to all! :D I am currently developoing an application that imports data from a CSV file. Each comma represents an array item that I need to extract data with. My problem is...
6
by: Senthil | last post by:
Code ---------------------- string Line = "\"A\",\"B\",\"C\",\"D\""; string Line2 = Line.Replace("\",\"","\"\",\"\""); string CSVColumns = Line2.Split("\",\"".ToCharArray());
12
by: Jeff S | last post by:
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant. PopupLink = "javascript:PopUpWindow(" &...
1
by: Mark Rae | last post by:
Hi folks, Apologies - am having a bit of a "senior moment"... Is there a way to convert a string containing escaped characters into a verbatim string literal? I.e. converting something like...
16
by: Charles Law | last post by:
I have a string similar to the following: " MyString 40 "Hello world" all " It contains white space that may be spaces or tabs, or a combination, and I want to produce an array...
4
by: vighnesh | last post by:
Hello EveryOne In my project I have to parse a string in Quotes as without Quotes.I tried the following code but it didn't work to me. I again getting the string with Quotes, Can Anybody suggest...
9
by: a | last post by:
I need to write a regular expression to match a quoted string in which the double quote character itself is represented by 2 double quotes. For example: "beginning ""nested quoted string"" end"...
4
by: Michael Yanowitz | last post by:
Hello: If I have a long string (such as a Python file). I search for a sub-string in that string and find it. Is there a way to determine if that found sub-string is inside single-quotes or...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.