[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

searching a character array for a pattern

Posted on 2005-04-10
7
Medium Priority
?
254 Views
Last Modified: 2010-04-01
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.
0
Comment
Question by:ba272
  • 3
  • 2
  • 2
7 Comments
 
LVL 33

Assisted Solution

by:hongjun
hongjun earned 400 total points
ID: 13750264
0
 
LVL 33

Expert Comment

by:hongjun
ID: 13750273
#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
 
LVL 30

Accepted Solution

by:
Axter earned 1600 total points
ID: 13750324
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ba272
ID: 13750355
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
 

Author Comment

by:ba272
ID: 13750362
That's really a new question, so I'll open it now.

0
 

Author Comment

by:ba272
ID: 13750377
I was really looking for the index of the occurence, but the strstr() function was interesting.
0
 
LVL 30

Expert Comment

by:Axter
ID: 13750405
>>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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question