0

I have a long string and I want to display the first 50 characters of it (without including the HTML content). Can anyone suggest any method?

This is the code:

eBdb.EpubReader.Epub Epubobj = new eBdb.EpubReader.Epub(myPath);
litepub.Text = Epubobj.GetContentAsHtml();

In litepub.Text I am getting text along with HTML. But I want to display only first 50 characters from the result of that function.

user1509
  • 1,151
  • 5
  • 17
  • 45
  • Could you show us what you've done so far? The question isn't clear. What HTML content? – karan k May 22 '12 at 06:23
  • Try this [link][1] http://stackoverflow.com/questions/2038104/parsing-html-to-get-content-using-c-sharp [1]: http://stackoverflow.com/questions/2038104/parsing-html-to-get-content-using-c-sharp – JayOnDotNet May 22 '12 at 07:11

2 Answers2

1

try this:

Regex.Replace(source, "<.*?>", string.Empty).Substring(0,49);
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
  • You should never parse HTML with regular expressions. Take a look here: http://stackoverflow.com/questions/590747/using-regular-expressions-to-parse-html-why-not to see why. – npinti May 22 '12 at 06:28
1

Take a look at HTML Agility Pack. You could take a look here on more information on how to get started.

Community
  • 1
  • 1
npinti
  • 51,780
  • 5
  • 72
  • 96
  • But the output that I am getting is in string form. It is not an HTML file. – user1509 May 22 '12 at 06:38
  • @coder311: But from your question, it seems that your string contains HTML no? – npinti May 22 '12 at 06:39
  • It does contain HTML content in it. But it is form of a string, not a file. Can I use HTML Agility Pack for strings containing HTML? – user1509 May 22 '12 at 06:53
  • 1
    @coder311: Yes you can. Take a look here: http://stackoverflow.com/questions/2535878/can-i-use-html-agility-pack-for-this – npinti May 22 '12 at 06:55