CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2023
    Posts
    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?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,401

    Re: How do I detect a quoted substring in a string?

    And what does the str.find return?
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2018
    Posts
    122

    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.

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,826

    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:

    Code:
    class="wikitable"
    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.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured