Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

HTML
<!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
Updated 21-May-12 21:10pm
v3
Comments
VJ Reddy 22-May-12 2:42am    
Can you please post a sample content of the string.
Member 8491154 22-May-12 3:25am    
Posted.
CodingLover 22-May-12 3:09am    
Is that a static content?
Member 8491154 22-May-12 3:25am    
Its just a code sample that is coming in the string.
Technoses 22-May-12 3:46am    
show you content with writing type??what do you want actually??

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:
C#
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
 
Share this answer
 
v2
Comments
Member 8491154 22-May-12 6:26am    
Can you also tell how it can be done through jquery?
VJ Reddy 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.
please refer below link for html tag stripping.

for C# :

Convert HTML to Plain Text[^]

HTML Tag Stripper[^]

for SQL :
MS SQL Function[^]
 
Share this answer
 
v3
use jquery
use $("#areaid").text()
 
Share this answer
 
Comments
Member 8491154 22-May-12 2:40am    
what is "#areaid" ? Can you explain it in detail?
mr.priyank 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>
 
Share this answer
 
Comments
CHill60 31-May-15 12:55pm    
You're only 3 years late with this and regex has already been suggested. As this is to your own site, many will consider this post spam - please consider this before answering old posts
Quote:
<!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>










 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900