THUNDERGROOVE
THUNDERGROOVE

Reputation: 307

Using cout to print starting from a specific place

So lets say I had a string that was

<html>
<head><title>301 Moved Permanently</title><head>

and so I'm using the str.find(); to find where the title tag starts and it gives me the correct position but how would I go about printing just the

301 Moved Permanently

My code:

    string requestedPage = page.GetBody(); //Get the body of a page and store as string "requestedPage"

    int subFromBeg = requestedPage.find("<title>"); //Search for the <title> tag
    int subFromEnd = requestedPage.find("</title>"); //Search for the </title> tag



    std::cout << requestedPage; //before

    requestedPage.substr( subFromBeg, subFromEnd );

    std::cout << requestedPage; //after

Upvotes: 0

Views: 134

Answers (2)

Robᵩ
Robᵩ

Reputation: 168726

requestedPage.substr( subFromBeg, subFromEnd );

should be

requestedPage = requestedPage.substr( subFromBeg, subFromEnd );

std::string::substr doesn't modify the string, it returns a modified copy of the string.

Upvotes: 1

Matt
Matt

Reputation: 10564

substr is how I would do it. Something like cout << str.substr(str.find("title") + 6, 21); would get you a 21-character string starting at 6 characters after 'title' (hopefully, I counted my indices right, but you get the idea).

Upvotes: 0

Related Questions