Get first picture
To get the data is a piece of HTML text, maybe there are many pictures in this text, need to intercept a picture as a title, that is, to do the main map, then you can use the following method to get the first picture.
Sample code
#region get the first picture
///<summary>
///Get HTML text picture address
///</summary>
///<param name= " Content "></param>
///<returns></returns>/
///public
ArrayList Getimgurl ( String html)
{
ArrayList resultstr = new ArrayList ();
Regex r = new Regex (@ "] src=s* (?: ´ (? <src>[^´]) ´|" " (? <src>[^ ""]) "" | (? <src>[^>s])) S*[^>]*> ", regexoptions.ignorecase);/Ignore case
MatchCollection mc = r.matches (HTML);
foreach (Match m in MC)
{
resultstr.add (m.groups["src"]. Value.tolower ());
}
if (Resultstr.count > 0)
{return
resultstr;
}
else
{
resultstr.clear ();
Return ResultStr
}
}
#endregion
Note: The above returns a collection that ArrayList
contains all the img in the text src
, so that we can access the IMG src
Intercepting HTML text
Sometimes the data we get is a piece of HTML text, a part of the HTML text that needs to be intercepted as a summary of the content, and we can use the following method
Sample code
#region News Content Summary///<summary>///News content Summary///</summary>///<param name= "sstring" ></param>///&L T;param name= "Nleng" ></param>///<returns></returns> public string Getcontentsummary (string content, int length, bool striphtml) {if (string). IsNullOrEmpty (content) | |
Length = = 0) return "";
if (striphtml) {regex re = new regex ("<[^>]*>"); Content = Re.
Replace (Content, ""); Content = content. Replace ("", "").
Replace ("", ""); if (content).
Length <= length) return content; else return content.
Substring (0, length) "..."; } else {if (content).
Length <= length) return content;
int pos = 0, NPOs = 0, size = 0;
BOOL Firststop = False, Notr = False, Noli = false;
StringBuilder sb = new StringBuilder (); while (true) {if (pos >= content.)
Length) break; String cur = content.
Substring (POS, 1); if (cur = = "<") {StrinG next = content. Substring (POS 1, 3).
ToLower (); if (next. IndexOf ("p") = = 0 && next. IndexOf ("pre")!= 0) {NPOs = content.
IndexOf (">", POS) 1; else if (next. IndexOf ("/p") = = 0 && next. IndexOf ("/PR")!= 0) {NPOs = content.
IndexOf (">", POS) 1; if (size < length) sb.
Append ("<br/>"); else if (next. IndexOf ("br") = = 0) {NPOs = content.
IndexOf (">", POS) 1; if (size < length) sb.
Append ("<br/>"); else if (next. INDEXOF ("img") = = 0) {NPOs = content.
IndexOf (">", POS) 1; if (size < length) {sb. Append (content.
Substring (POS, npos-pos));
size = Npos-pos 1; } else if (next. IndexOf ("li") = = 0 | | Next. IndexOf ("/li") = = 0) {NPOs = content.
IndexOf (">", POS) 1; if (size < length) {sb. Append (content.
Substring (POS, npos-pos)); else {if (!noli && next.) IndexOf ("/li") = = 0) {sb. Append (content.
Substring (POS, npos-pos));
Noli = true; else if (next)}}. IndexOf ("tr") = = 0 | | Next. IndexOf ("/tr") = = 0) {NPOs = content.
IndexOf (">", POS) 1; if (size < length) {sb. Append (content.
Substring (POS, npos-pos)); else {if (!notr && next.) IndexOf ("/tr") = = 0) {sb. Append (content.
Substring (POS, npos-pos));
NOTR = true; else if (next)}}. IndexOf ("td") = = 0 | | Next. IndexOf ("/td") = = 0) {NPOs = content.
IndexOf (">", POS) 1; if (size < length) {sb. Append (content. SubstRing (POS, npos-pos)); } else {if (!NOTR) {sb. Append (content.
Substring (POS, npos-pos)); }} else {NPOs = content.
IndexOf (">", POS) 1; Sb. Append (content.
Substring (POS, npos-pos));
} if (NPOs <= pos) NPOs = pos 1;
pos = NPOs; else {if (size < length) {sb.
Append (cur);
size; } else {if (!firststop) {sb.
Append ("...");
Firststop = true;
}} POS; } return SB.
ToString (); }} #endregion
Summarize
The above is the use of C # to get a section of HTML text in the first picture and intercept the content of the summary of the entire content, I hope the content of this article for everyone to learn or use C # can help, if there are questions you can message exchange, thank you for the cloud Habitat Community support.