Splitting a String

I'm trying to do something simple. I have a phone number, and I'd like to parse it into different parts of it.

 
string phone_number = 523-9872*8536;


What I'd like to do is parse the string different ways.

Method 1:

I want it to return 123

Method 2:

I want it to return everything between the '-' and '*'. Perhaps this would be also starting at element 4 (the 9) and ending at character space 6 (the 2).

How can I do this?
A simple but tedious way to parse a string is simply to go through each character, one at a time, checking to see if they match what you want.

1
2
3
4
5
6
7
8
9
10
11
string phone_number = "523-9872*8536";

for( int i = 0; i < phone_number.length(); i++ ) {
    char c = phone_number[i];
    
    if( c == ... )
        // do one thing
    else
        // do something else

}
closed account (j3Rz8vqX)
Yup what Matthead said

1
2
3
4
5
6
7
8
   //start_parse_function(&index,&theStr)
      //start_loop(index;index< phone_number.length(); index++)
         //if character is a valid character AKA >= '0' && <='9'
             theStr = theStr.c_str()+/*character*/;
         //else
            //break; done for this part
      //end_loop
   //end_parse_function 

Topic archived. No new replies allowed.