Extracting specific parts from a string

This is a discussion on Extracting specific parts from a string within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a string like this: "FROM:<mail@server.com>" I need to extract 2 parts from this string(mailbox and server) and ...

  1. #1
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339

    Extracting specific parts from a string

    Hi,

    I have a string like this:

    "FROM:<mail@server.com>"

    I need to extract 2 parts from this string(mailbox and server) and put them in two new strings as follows:

    stringa = "mail"
    stringb = "server.com"

    all other bits can be discarded.

    I would be very gretful if anyone has some code for extracting these two bits either side of the @ symbol, please remember any email address could be in there.

    Thanks
    TNT
    TNT
    You Can Stop Me, But You Cant Stop Us All

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Do you have Boost? It has string tokenizer methods that can easily do what you want and are probably faster than rolling your own.

    Otherwise, you need to find the first < and discard everything before and including it; find the last > and discard it and anything after, and then split into 2 at the @ symbol.

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Or in C++:
    Code:
    std::string s = "FROM:<mail@server.com>";
    
    int ltPos = s.find('<');
    int atPos = s.find('@');
    int gtPos = s.find('>');
    
    std::string user = s.substr(  ltPos+1, atPos-ltPos-1 );
    std::string host = s.substr( atPos+1, gtpos-atPos-1 );
    I don't have the time to test this code, but something along these lines should work.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    12

    Smile

    Code:
    This piece of code does what you are asking.
    #include <iostream.h>
    #include <conio.h>
    char* stringy="FROM:<mail@server.com>" ;
    char string1[30];
    char string2[30]; 
    int slen;
    
    int main()
    	{
     		slen=strlen(stringy);
     		int fromhere=0;
     		int j=0; 
     		
     			for(int i=0;i<slen;i++)
     				{
       					string1[i-6]=stringy[i];
       						if(stringy[i]=='@')
       							{
       								slen=1; 
     							    string1[i-6]='\0';
                                }
    				}
    		slen=strlen(stringy);  
     			for(int i=0;i<slen;i++)
     				{ 
    					if(stringy[i]=='@' )
    						{
    							fromhere=1;
    						}	   
    					if(fromhere==1)
    						{
    							string2[j]= stringy[i+1];
     							j++;
    						}   
     				}
    		string2[strlen(string2)-1]='\0'; 
      		cout<<string1<<endl;
      		cout<<string2;
     
        	getch();
         	return 0;
    	}
    Last edited by bigwullie; 07-10-2003 at 06:03 PM.

  5. #5
    ___
    Join Date
    Jun 2003
    Posts
    806
    I checked it out from curiosity and got these errors from bigwillie.

    Code:
    c:\compiled\extract\cpp1.cpp(11) : error C2065: 'strlen' : undeclared identifier
    c:\compiled\extract\cpp1.cpp(25) : error C2374: 'i' : redefinition; multiple initialization
    I don't know what they mean but thats the 2 errors I got
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    12
    c:\compiled\extract\cpp1.cpp(11) : error C2065: 'strlen' : undeclared identifier
    c:\compiled\extract\cpp1.cpp(25) : error C2374: 'i' : redefinition; multiple initialization

    You get these errors if you compile the previous code with MSVisualC++

    If you use the Borland compiler there is no problem

    The following code works with MSVisualC++

    Code:
    #include <iostream.h>
    #include <conio.h> 
    #include <string.h>
    
    char* stringy="FROM:<mail@server.com>" ;
    char string1[30];
    char string2[30]; 
    int slen;
    
    int main()
    	{
     		slen=strlen(stringy);
     		int fromhere=0;
     		int j=0; 
     		
     			for(int i=0;i<slen;i++)
     				{
       					string1[i-6]=stringy[i];
       						if(stringy[i]=='@')
       							{
       								slen=1; 
     							    string1[i-6]='\0';
                                }
    				}
    		slen=strlen(stringy);  
     			for(int k=0;k<slen;k++)
     				{ 
    					if(stringy[k]=='@' )
    						{
    							fromhere=1;
    						}	   
    					if(fromhere==1)
    						{
    							string2[j]= stringy[k+1];
     							j++;
    						}   
     				}
    		string2[strlen(string2)-1]='\0'; 
      		cout<<string1<<endl;
      		cout<<string2<<endl;
     
        
         	return 0;
    	}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  2. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  3. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 01:54 PM
  4. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM
  5. Replies: 2
    Last Post: 05-05-2002, 01:38 PM

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