Reputation: 10010
I want to assign HTML snippet to string variable. something like -
string div = "<table><tr><td>Hello</td></tr></table>"; // It should return only 'Hello'
Please suggest.
Upvotes: 0
Views: 1731
Reputation: 1561
string div = "<table><tr><td>Hello</td></tr></table>"; // It should return only 'Hello
var doc = new XmlDocument();
doc.LoadXml(div);
string text = doc.InnerText;
Do you also need the Jquery version of this?
Upvotes: 1
Reputation: 140753
If you are sure that the HTML won't change between the string you want to get, you can simply do a Substring between the two constants string and you will get your string into your variable.
const string prefix = "<table>";
const string suffix = "</table>";
string s = prefix + "TEST" + suffix ;
string s2 = s.Substring(prefix.Length, s.IndexOf(suffix, StringComparison.Ordinal) - prefix.Length);
Here is the Regex version:
const string prefix = "<table>";
const string suffix = "</table>";
string s = prefix + "TEST" + suffix;
string s2 = Regex.Match(s, prefix + "(.*)" + suffix).Groups[1].Value;
Upvotes: 1