Asp.net (C #) function for obtaining the address of the first image

Source: Internet
Author: User

First, find the location of the first

Then, separate spaces to obtain the attributes and attribute values of the image. The extracted SRC value is the image address.

CodeAs follows:

Copy code The Code is as follows: // <summary>
/// Obtain the image address in the text
/// </Summary>
/// <Param name = "content"> content </param>
/// <Returns> address string </returns>
Public static string getimageurl (string content)
{
Int mouse = 0;
Int cat = 0;
String imagelabel = "";
String imgsrc = "";
String [] attributes;

Do // get the connection of the first image as the main Image
{
Cat = content. indexof ("Mouse = content. indexof ('>', CAT );
Imagelabel = content. substring (CAT, mouse-cat); // Image Tag

Attributes = imagelabel. Split (''); // separate image attributes

Foreach (string temp_attributes in attributes) // obtain the image address attribute
If (temp_attributes.indexof ("src")> = 0)
{
Imgsrc = temp_attributes.tostring ();
Break;
}
Imgsrc = imgsrc. substring (imgsrc. indexof ('"') + 1, imgsrc. lastindexof ('"')-imgsrc. indexof ('"')-1); // extract the address from the cluster address attribute

} While (imgsrc = "" & CAT> 0 );

Return (imgsrc );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.