Hello everyone,

I downloaded the source of an internet file and need to filter a specific part of it.

I know how the string::size_type position = Line.find("World:</TD><TD>"); function works, but this only tells me where the data I am looking for is located (the data is after "World:</TD><TD>").

I want to have everything that is behind it in a different string, until the "<" gets reached.

Example:

</TR><TR BGCOLOR=#F1E0C6><TD>World:</TD><TD>Blahblah</TD></TR>

is loaded into my string "Line".
I run my sorting thing and it should return "Blahblah" in an other string.

It is an std::string type variable.

I am also looking for a way to convert from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const
char *'

(at least, the error message says so, it is just a string to const char* conversion)


Thanks in advance.
Eddy

Dani AI

Generated

The original question from was to pull the cell text after the marker (the "World:</TD><TD>" cell) and hand it to C APIs as a const char*. showed a working find/substring approach and confirmed string.c_str() for the C pointer. Below are short, practical alternatives and pitfalls to avoid when extracting text from HTML-like strings.

A minimal, robust extractor that stops at the next < (so it tolerates varied closing tags) and trims whitespace:

std::string extractCellValue(const std::string& line, const std::string& key)
{
    auto start = line.find(key);
    if (start == std::string::npos) return {};
    start += key.size();

    auto end = line.find('<', start);
    if (end == std::string::npos) end = line.size();

    std::string out;
    out.assign(line.begin() + start, line.begin() + end);

    // simple trim
    auto left = out.find_first_not_of(" \t\r\n");
    if (left == std::string::npos) return {};
    auto right = out.find_last_not_of(" \t\r\n");
    out.assign(out.begin() + left, out.begin() + right + 1);

    return out;
}

If the markup varies, a single regex can be neater (for well-formed, predictable fragments):

std::regex rx(R"(World:</TD><TD>([^<]+))");
std::smatch m;
if (std::regex_search(line, m, rx)) {
    std::string cell = m[1];
}

Practical tips

  • Ad-hoc string parsing is brittle: attributes, extra whitespace, or nested tags break simple searches. For production parsing, use a real HTML parser (libxml2, Gumbo, etc.).
  • When passing to C APIs, std::string::c_str() (or data() in modern C++) gives a pointer; that pointer is invalidated if the string is modified or destroyed. See std::string::c_str and std::string::data for details.
  • If values may contain HTML entities (ampersands, nonbreaking spaces), decode them before use.

Recommended Answers

All 2 Replies

Is this anything like you're trying to do?

#include <iostream>
#include <string>
using namespace std;

bool foo(string &result, const string &Line)
{
   static const string keystart("World:</TD><TD>"), keyend("</TD></TR>");
   string::size_type start = Line.find(keystart);
   if ( start != string::npos )
   {
      start += keystart.length();
      string::size_type end = Line.find(keyend, start);
      if ( end != string::npos )
      {
         result = Line.substr(start, end - start);
         return true;
      }
   }
   return false;
}

int main()
{
   string Line("</TR><TR BGCOLOR=#F1E0C6><TD>World:</TD><TD>Blahblah</TD></TR>");
   string text;
   if ( foo(text, Line) )
   {
      cout << text << '\n';
   }
   return 0;
}

/* my output
Blahblah
*/

EDIT:
I did it. String to const char* conversion can be done with string.c_str() Thanks for your help Dave! I really appreciate it

Mwah... Looking pretty good... I'll try it.

Thanks for your reply.

I'm still having problems converting a string to a const char*.


Thanks again,
Eddy
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.