There are many ways of doing this in C++....here are two examples, one using c style strings and another using std::string:
NOTE: Error / Exception checking should be added.Code:#include <cstdlib> #include <cstdio> #include <cstring> #include <string> using namespace std; int main( int argc, char** argv ) { //---------------------------------------------------------------- //-- using old c string routines //---------------------------------------------------------------- char strResult[100] = { NULL }; int strPosition = 0; char* testValue = "dslb-012-034-567-089.pools.myisp-ip.net"; //-- get the position of the first dash and period char* strFromFirstDash = strchr( testValue, '-' ); char* strFromFirstPeriod = strchr( testValue, '.' ); //-- now find out how long the first string section is strPosition = (strFromFirstDash -testValue); //-- build the new string strncpy( strResult, testValue, strPosition ); strncat( strResult, strFromFirstPeriod, (99 - strPosition )); printf( "Using C string manipulation - %s\n", strResult ); //---------------------------------------------------------------- //-- using std::string //---------------------------------------------------------------- string stringValue = "dslb-012-034-567-089.pools.myisp-ip.net"; //-- get the position of the first dash and period int dashPosition = stringValue.find_first_of( '-' ); int periodPosition = stringValue.find_first_of( '.' ); //-- build the new string removing the section we don't want string stringNewValue = stringValue.substr(0, dashPosition ); stringNewValue.append( stringValue.substr( periodPosition, stringValue.length() - periodPosition)); printf( "Using std::string - %s\n", stringNewValue.c_str() ); return 0; }
HTH




Reply With Quote