Tom Rider
Tom Rider

Reputation: 2805

Issue with string in c#?

I am creating a string in c#.

string jsVersion = @"function getContent() {
                     var content = " + "\"" + documentString + "\"" + @"
                     return content; 
                     }";

The documentString variable contains a huge string which have line breaks also. Now in javascript when i load this string the content variable does not contains a valid string (because of line breaks).

Now how can i create a string which is valid even if there is line breaks ?

Upvotes: 1

Views: 109

Answers (4)

Moriya
Moriya

Reputation: 7906

If you want to get rid of the line breaks just remove @ from the sting.

@ acts a verbatim for your string and therefor adds the line breaks you entered with your declaration.

string jsVersion = "function getContent() {
                     var content = " + "\"" + documentString + "\"" + "
                     return content; 
                     }";

should do the trick.

Upvotes: 0

Pranav
Pranav

Reputation: 8871

This will replace your line breaks with <br/>:-

stringToDecode.replace(/\n/, '&lt;br /&gt;')

Upvotes: 0

Srikanth Venugopalan
Srikanth Venugopalan

Reputation: 9049

Can you use string.format instead of concatenation in this fashion?

An example:

string jsVersion = string.format("function getContent() {var content = '{0}'return content; }",documentString);

Upvotes: 1

ybo
ybo

Reputation: 17152

You can use HttpUtility.JavaScriptStringEncode

Upvotes: 2

Related Questions