Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# HTML string ASP.NET4.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?
 
Some sample HTML code:
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
			<html>
			   <head>
				  <title>Paula - Microsoft Word - Comparison of the different image compression algorithms.doc</title>
				  <title></title><link href="/DigitalLibrary/extData.aspx?filePath=stylesheet.css&epub=b3aab940-fb48-4f6c-ae63-d599f4893795_aguilera_rpt.epub" type="text/css" rel="stylesheet"/>
			   </head>
			   <body>
				  
      <div class="body">
         <div id="frontmatter">
            <div id="titlepage">
            </div>    
         </div>
      </div>
   
 
<a id="1"></a><p><pre> 
Comparison of different image
compression formats 
 
 
Posted 21-May-12 21:27pm
Edited 21-May-12 22:10pm
v3
Comments
VJ Reddy at 22-May-12 2:42am
   
Can you please post a sample content of the string.
Member 8491154 at 22-May-12 3:25am
   
Posted.
CodingLover at 22-May-12 3:09am
   
Is that a static content?
Member 8491154 at 22-May-12 3:25am
   
Its just a code sample that is coming in the string.
Technoses at 22-May-12 3:46am
   
show you content with writing type??what do you want actually??
VJ Reddy at 28-Jun-12 9:53am
   
Thank you for viewing and accepting the solution :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

jQuery is much powerful to extract the content of HTML document.
 
However, if you can't use jQuery then the Regex class can be used to extract the content between <title> and </title>, which is required as mentioned in the question, as shown below:
string htmlText = @"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.1//EN"" ""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"">
            <html>
               <head>
                  <title>Paula - Microsoft Word - Comparison of the different image compression algorithms.doc</title>
                  <title></title><link href=""/DigitalLibrary/extData.aspx?filePath=stylesheet.css&epub=b3aab940-fb48-4f6c-ae63-d599f4893795_aguilera_rpt.epub"" type=""text/css"" rel=""stylesheet""/>
               </head>
               <body>
                <div class=""body"">
                    <div id=""frontmatter"">
                        <div id=""titlepage"">
                        </div>
                    </div>
                </div>
            <a id=""1"">";
 
    Match match = Regex.Match(htmlText,@"<title>([^<>]*)</title>",
                RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
 
    if (match.Success && match.Groups.Count > 1)
        Console.WriteLine(match.Groups[1].Value);
 
//Output
//Paula - Microsoft Word - Comparison of the different image compression algorithms.doc
  Permalink  
v2
Comments
Member 8491154 at 22-May-12 6:26am
   
Can you also tell how it can be done through jquery?
VJ Reddy at 22-May-12 7:45am
   
$('title').html(); can be used to get the content of title tag.
To test it, visit the following page
http://www.learningjquery.com/2006/12/jquerify-bookmarklet
click on jQuerify link on the page.
Then enter $('title').html(); in the javascript console of the brower and press enter.
Visit http://jquery.com/ for full details of jQuery.
Thank you.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

please refer below link for html tag stripping.
 
for C# :
 
Convert HTML to Plain Text[^]
 
HTML Tag Stripper[^]
 
for SQL :
MS SQL Function[^]
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

use jquery
use $("#areaid").text()
  Permalink  
Comments
Member 8491154 at 22-May-12 2:40am
   
what is "#areaid" ? Can you explain it in detail?
priyankdotnet at 22-May-12 3:55am
   
areaid is the id of the div ( body etc ) that contains the html.
this will be done in javascript.
<div id="areaid"> Your Html Content </div>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,327
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,920


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 22 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100