3

In my project, I need to read some URLs and store the starting tags into some variables, but the project won't compile. May be, its because I am not using the assignment to the string correctly. Following is what i tried and got the compile error

string startTag = "<span id="productLayoutForm:OurPrice" class="pdp_details_hs18Price" itemprop="price">";

string anotherStartTag = "<span class="price final-price our fksk-our" id="fk-mprod-our-id">Rs.<span class="small-font">"

Please tell, what should be the correct code for above and where can I learn how to store such HTMLs into string or how to use string for such puposes.

5
  • 2
    You need to escape the " symbol Commented Jan 16, 2012 at 7:08
  • @Francois, I know it's basic but I wasn't able to search out a good documentation to understand things fast....sorry Commented Jan 16, 2012 at 7:16
  • Pankaj, am I correct to assume that you seek to put values from codebehind into some HTML? Or do you want the string above to reflect exactly as it is in HTML? Commented Jan 16, 2012 at 7:21
  • @FrankAllenby, Actually what I am doing is visiting some URL and then extracting the values from certain Divs or spans.....And then rendering those fetched values in my rendered View Commented Jan 16, 2012 at 7:30
  • @Pankaj, Mhmm okay. I would suggest, then, that you append the proper strings to one another using an addition operator(+) e.g. string1 + " " + string2, etc. so that you can include other values in the string. Also, you should escape the quotes as others have suggested for static string values such as style names(if they are static). It may be dangerous to extract values from certain divs, as any change made to a name or ID of that div can break your code. But that's beyond the scope of this question :). Commented Jan 16, 2012 at 7:34

2 Answers 2

4

You need to "escape" the quotes in your strings, for example:

string startTag = "<span id=\"productLayoutForm:OurPrice\" class=\"pdp_details_hs18Price\" itemprop=\"price\">";

The \ before the quotes that are inside the string tells the C# compiler that the quotes are part of the string and not the beginning/end of the string.

Sign up to request clarification or add additional context in comments.

5 Comments

Can we use single quotes '. Will it make any difference ?
@PankajUpadhyay - I'd imagine you have a copy of Visual Studio somewhere, try it and see! =) That said, the single quote ' is used in C# to denote char so won't work. C# doesn't work in the same way as Javascript =)
Yes, single quotes should work as well :) if you're talking about single quotes inside the string and not around
@Rob I believe sjums is referring the single quote within it should work as in 'productLayoutForm:OurPrice', javascript won't mind it nor would C#
if it helps, acccept it as anwser :)
1

The " sign indicates the start and end of a string.so to use it in the middle of a string you have to escape it, do that by setting a backslash in front of it.. Like this: \"

Comments

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.