Solved

how can i parse part of string ? (urgent)

Posted on 2004-09-19
5
160 Views
Last Modified: 2010-04-01
Hi experts,

I have a string, like " this is a customer account, ref: abc#123456789 ".
I only need to parse partial string "ref: abc#123456789". how can i do that ?
Any function can do that ?

Thanks
0
Comment
Question by:justinY
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 12097318
You'd find the start of the substring by e.g. looking for 'ref:', like

char acSomeString[] = " this is a customer account, ref: abc#123456789 ";

char* pszStringToParse = strstr ( acSomeString, "ref:");

// will then be "ref: abc#123456789 "
0
 
LVL 1

Expert Comment

by:zekard
ID: 12098959
justinY,
try this:

#include <iostream>

using namespace std;

char* FindSubStr ( char *str, int size, char *query )
{
      int IndexOfSubStr = 0;

      // find occurence of substr
      for ( int i = 0; i < size; i++ )
      {
            if ( str[i] == query[0] && str[i+1] == query[1] && str[i+2] == query[2] )
            {
                  IndexOfSubStr = i;
                  break;
            }
      }

      static char substr[128];
      int substrIndex = 0;
      // copy string to substr and return it
      for ( i = IndexOfSubStr; i < size; i++ )
      {
            substr[substrIndex] = str[i];
            substrIndex++;
      }

      return substr;
}
int main ()
{
      // input string
      char str[128] = "this is a customer account, ref: abc#123456789";
      
      // substr stores the sub string if found
      char *substr;
      
      // size of input string
      int size = strlen ( str );

      // call function to find our string
      substr = FindSubStr ( str, size, "ref:" );

      cout << substr << endl;

      return 0;
}
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 12099065
More C++ way of doing it

#include <iostream>
#include <string>

using namespace std ;
int main () {

      string s1 = "this is a customer account, ref: abc#123456789 ";

      unsigned int pos = s1.find ( "ref" ) ;
      cout << s1.substr ( pos ) ;
      system ( "PAUSE" ) ;
}
0
 

Author Comment

by:justinY
ID: 12150602
Thank you all,
Lets change my question a little bit.

string s1 = "this is a customer account,  'abc#123456789' ";
so i want to extract substring "abc#123456789" from single quotes (' ')
i like to use
s = s.substr(s.find_first_of("\'") + 1);
s = s.substr(s.find_last_of("\'") - 1);
My code is like the followings. but i have compiling errors, any experts can help ? or any function of extracting substring from single quotes (' ') i can use ?
 
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <algorithm>

using namespace std;

main(int arc, char *arv[])
{
      ifstream fin("fin.txt");
      ofstream fout("fout.txt");
      char row_read[512];
      char row_elem[50][50];
      int i =0;
while( fin.getline( row_read, sizeof( row_read ) ) )
{
      //////////////////////////////////////////
      // get tokens, parse string with ","
      //////////////////////////////////////////
      char *token = strtok( row_read, ',' );
      char *subtoken(token);
      while( token != NULL )
      {
            
           subtoken = token.substr(token.find_first_of("\'") + 1);
           subtoken = token.substr( token.find_last_of("\'") - 1);
            /////////////////////////////////////////////////////////////
            //write the subtoken into row_elem[i]
            ////////////////////////////////////////////////////////////
             /* While there are tokens in "string" */
                   strcpy(row_elem[i], subtoken );
             /* Get next token: */
                   token = strtok( NULL, ',' );
                   i++;
                  
            }
            
      }

                    fout<<setw(20)<< row_elem[8]<<endl;

     i=0;
}

return 0;
}
0

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

615 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