2
\$\begingroup\$

Is this the easiest way to delete everything between and including the two first quotes (if present) in an std::wstring?

unsigned int firstQuotePos = logFileName.find_first_of(L"\"");
unsigned int secondQuotePos = logFileName.find_first_of(L"\"", firstQuotePos + 1);

if (firstQuotePos != std::wstring::npos && secondQuotePos != std::wstring::npos)
{
    logFileName = std::wstring(logFileName.begin(), logFileName.begin() + firstQuotePos)
        + std::wstring(logFileName.begin() + secondQuotePos + 1, logFileName.end());
}
\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

C++ is very picky about types.
You should always try and use the correct ones:

unsigned int firstQuotePos = logFileName.find_first_of(L"\"");
unsigned int secondQuotePos = logFileName.find_first_of(L"\"", firstQuotePos + 1);

should be:

std::string::size_type firstQuotePos  = logFileName.find_first_of(L"\"");
std::string::size_type secondQuotePos = logFileName.find_first_of(L"\"", firstQuotePos + 1);

Next there is already an erase that does this:

if (firstQuotePos != std::string::npos)
{
    logFileName.erase(firstQuotePos, (secondQuotePos - firstQuotePos));
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.