-
March 24th, 2023, 11:52 PM
#1
How do I detect a quoted substring in a string?
am trying to determine if a string contains a quoted substring.
This is my code that gives me a compile error: "ai3.cpp:162:37: warning: character constant too long for its type"
Code:
//if the line contains 'class="wikitable"'
string findString = 'class="wikitable"';
size_t pos = str.find(findString);
if (pos != string::npos) {
cout << "Substring found at position " << pos << endl;
}
I've tried:
Code:
"'class=\"wikitable\"'"
but str.find doesn't find my substring.
So, can anyone suggest how I may search for the presence of a quoted substring?
-
March 25th, 2023, 03:27 AM
#2
Re: How do I detect a quoted substring in a string?
And what does the str.find return?
Victor Nijegorodov
-
March 25th, 2023, 04:37 AM
#3
Re: How do I detect a quoted substring in a string?
> This is my code that gives me a compile error: "ai3.cpp:162:37: warning: character constant too long for its type"
The outer string needs to be in double quotes, and the inner ones escaped.
Like so
Code:
string findString = "class=\"wikitable\"";
This isn't Python, where '' and "" are equivalent.
-
March 25th, 2023, 05:18 AM
#4
Re: How do I detect a quoted substring in a string?
or you can use raw strings
Code:
#include <iostream>
#include <string>
int main() {
const std::string findString { R"(class="wikitable")" };
std::cout << findString << '\n';
}
which displays:
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.2)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|