Thread: C++ Extracting url from string problem

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    7

    Exclamation C++ Extracting url from string problem

    Hey, how would you extract a URL and another string from a string? I believe you use the find string, however how can you stop until '.com' do you have to use the substring method where you count the lenth of the url or can you combine 2 finds? e.g. find 'http://' and '.com'?

    for instance:

    Code:
    string hyperlink = "<a href=http://www.ilovedonuts.com>Donut World!!</a>";
    
    findurl = hyperlink.find("http://");
    ...
    ....
    .....
    Thanks

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Analize this code:
    Code:
    string hyperlink = "<a href=http://www.ilovedonuts.com>Donut World!!</a>";
    string beginStr = "http://", endStr = ".com";
    unsigned int beginPos = hyperlink.find(beginStr), endPos = hyperlink.find(endStr), invalidPos = hyperlink.npos;
    if (beginPos != invalidPos && endPos != invalidPos && endPos > beginPos)
    {
        unsigned int urlLen =  endPos - beginPos + endStr.length();
        string urlStr = hyperlink.substr(beginPos, urlLen);
        cout << "Found URL " << urlStr;
    }
    else
    {
        cout << "No valid URL found";
    }
    Last edited by DRK; 11-12-2012 at 03:36 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 06-16-2011, 06:21 PM
  2. extracting char from a string
    By roaan in forum C Programming
    Replies: 12
    Last Post: 07-25-2009, 03:03 PM
  3. extracting from a string.
    By shabbirhussain in forum C Programming
    Replies: 11
    Last Post: 08-10-2008, 02:04 PM
  4. extracting specify int in a string
    By lingz82 in forum C++ Programming
    Replies: 5
    Last Post: 09-06-2006, 10:18 AM
  5. Extracting a sub-String
    By mikem22 in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 08:49 AM

Tags for this Thread