0

First off, forgive me for I know very little about MS Visual Studio & ASP, but my issue is that I can not seem to figure out the syntax for placing a variable within a string in the following ASP code....

    description = viewCat[0]["description"].ToString();

metaHTML = @"
    <title></title>
    <meta name='description' content='" + description + "' />
";

Question I'm looking for is what is the correct way of placing the variable "description" within the string??

1
  • Can you describe what's going wrong with the code you're using? Commented Aug 5, 2014 at 13:57

3 Answers 3

1

To create a string from a variable, you can use String.Format:

string description = "something";
string formattedStr = String.Format("Some string and {0}",description);

In your case:

description = viewCat[0]["description"].ToString();
metaHTML = String.Format("<title></title><meta name='description' content='{0}'/>", description);
Sign up to request clarification or add additional context in comments.

Comments

1

Your assignment of metaHTML comprises three strings - two literals and the variable description. Only the first literal is a multiline string (you prefix it with @). The second string is not: the characters "' /> are an unterminated string, and aren't correlated with the characters on the next line.

What you want to do is this - note the extra @:

metaHTML = @"
    <title></title>
    <meta name='description' content='" + description + @"' />
";

1 Comment

Awesome! Thank you so much, Dan... this helps me out a lot!
0

What is the error that you get since this is a nice way of doing the stuff that you're talking about.

@"<title></title>
<meta name='description' content='" + description + "' />";

The only thing that you must worry about is any un escapted " inside the string. On the first occurance this string would break the string. Otherwise it would work.

Red squiggly lines in VS

In the Visual Studio the Red Squiggly lines are due to the error in the String length. In ASP.NET and C# to create multiline Strings you use @ before the string to make it multiline. Like the first line of your String value.

Otherwise each string is single line and you cannot start a new line in it like you're doing right now. So either use this

<meta name='description' content='" + description + "' />";

... or else use this

<!-- note the @ in the string -->
<meta name='description' content='" + description + @"' />
";

The better function is to use the first code I provided, because it ends the line where it must be ended. Try it, it would remove the red lines.

3 Comments

Well, It seems when I place that variable as it is, Visual Studio indicates that something might be wrong by placing "squiggly" lines under each character after the "+ description +" variable.... make sense?
Yes it does. Wait let me write it out.
@GreggMoore, have a look now. It explains those lines.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.