need a instring command

This is a discussion on need a instring command within the C++ Programming forums, part of the General Programming Boards category; I'am looking for an command to test if var1 isin var2 e.g. when var1 is "This is a test" and ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    need a instring command

    I'am looking for an command to test if var1 isin var2 e.g. when var1 is "This is a test" and var2 is "test" the command should give back true or 1 or something like that.
    Any ideas?

    THX

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,037
    Code:
    for (int i = 0; i < 1 + var1.length() - var2.length(); i++)
       if (var1.substr(i,var2.length()) == var1)
          return true;
    Wow, same code in two posts in a row. Let's keep this train running.
    Sent from my iPadŽ

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,673
    This is what the string's find member function is for:

    Code:
    string var1 = "This is a test";
    string var2 = "test";
    
    if( var1.find(var2) != string::npos )
        cout << "Found it." << endl;
    else
        cout << "Couldn't find it." << endl;
    I used to be an adventurer like you... then I took an arrow to the knee.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,037
    Oh, what's the point of programming? Everything's already been programmed already.

    [EDIT]
    I make redundant people look not reduntant.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. problem with "touch" command in c program
    By Moony in forum C Programming
    Replies: 10
    Last Post: 08-01-2006, 09:56 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Ping problem
    By bladerunner627 in forum C++ Programming
    Replies: 12
    Last Post: 02-02-2005, 11:54 AM
  5. exe files in -c- language
    By enjoy in forum C Programming
    Replies: 6
    Last Post: 05-18-2004, 04:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21