searching a character array for a pattern

Hi,

It's been a while since I've written in C++ and I'm spoiled by C# powerful string object.  What's the best function to use to search for the first index of "\Debug" within a character array?

Thanks.
ba272Asked:
Who is Participating?
 
AxterConnect With a Mentor Commented:
You can use the find member function of string.
Example code:
#include <iostream>
#include <string>
using namespace std;



int main(){
      string data = "this is a test \\Debug abc 123 xxxx";

      size_t f = data.find("\\Debug");

      if (f != string::npos)
      {
            cout << "Found key word at " << endl;
            cout << data.substr(f) << endl;
      }
0
 
hongjunCommented:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main( void )
  {
    if ( strstr("This is an example \Debug", "\Debug") == NULL )
        printf( "not found");
    else
        printf("FOUND");

    return EXIT_SUCCESS;
  }



hongjun
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ba272Author Commented:
That's great. It works.  I never used strings in C++, it was always arrays of characters.

So could you tell me how to copy everything to the left of the data.substr(f) to a new string?

I appreciate your help.
0
 
ba272Author Commented:
That's really a new question, so I'll open it now.

0
 
ba272Author Commented:
I was really looking for the index of the occurence, but the strstr() function was interesting.
0
 
AxterCommented:
>>So could you tell me how to copy everything to the left of the data.substr(f) to a new string?

Sure.

Example:
string SubData = data.substr(f);
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.