Thread: need a instring command

  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,079
    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,817
    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;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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, 12:54 PM
  5. exe files in -c- language
    By enjoy in forum C Programming
    Replies: 6
    Last Post: 05-18-2004, 04:36 PM