Click here to Skip to main content
15,898,874 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionstring trim and toupper/tolower Pin
George_George4-Sep-08 19:47
George_George4-Sep-08 19:47 
AnswerRe: string trim and toupper/tolower Pin
Nibu babu thomas4-Sep-08 19:59
Nibu babu thomas4-Sep-08 19:59 
GeneralRe: string trim and toupper/tolower Pin
George_George4-Sep-08 20:04
George_George4-Sep-08 20:04 
GeneralRe: string trim and toupper/tolower Pin
Nibu babu thomas4-Sep-08 21:03
Nibu babu thomas4-Sep-08 21:03 
GeneralRe: string trim and toupper/tolower Pin
George_George4-Sep-08 21:40
George_George4-Sep-08 21:40 
GeneralRe: string trim and toupper/tolower Pin
Nibu babu thomas4-Sep-08 21:48
Nibu babu thomas4-Sep-08 21:48 
GeneralRe: string trim and toupper/tolower Pin
George_George4-Sep-08 21:58
George_George4-Sep-08 21:58 
GeneralRe: string trim and toupper/tolower Pin
Nibu babu thomas4-Sep-08 22:19
Nibu babu thomas4-Sep-08 22:19 
George_George wrote:
1.

No document which describes all possible exceptions may be thrown by a specific STL function, like string.substr?

2.

"the files exception and stdexcept" -- yes, but for a specific STL function like string.substr, how do you check all possible exceptions may be thrown by looking at the file? Smile


I am not sure where you can find them, but here is a more secure version of Trim.

void Trim( const std::string& StrToTrim, std::string& TrimmedString )
{
   // Find first non whitespace char in StrToTrim
   std::string::size_type First = StrToTrim.find_first_not_of( ' ' );
   // Check whether something went wrong?
   if( First == std::string::npos )
   {
      First = 0;
   }

   // Find last non whitespace char from StrToTrim
   std::string::size_type Last = StrToTrim.find_last_not_of( ' ' );
   // If something didn't go wrong, Last will be recomputed to get real length of substring
   if( Last != std::string::npos )
   {
      Last = ( Last + 1 ) - First;
   }

   // Copy such a string to TrimmedString
   TrimmedString = StrToTrim.substr( First, Last );
}


Earlier function will throw an out_of_range exception if we gave a string like " " for trimming. This one doesn't. Wink | ;)


Nibu babu thomas
Microsoft MVP for VC++


Code must be written to be read, not by the compiler, but by another human being.

Programming Blog: http://nibuthomas.wordpress.com

GeneralRe: string trim and toupper/tolower Pin
George_George4-Sep-08 22:47
George_George4-Sep-08 22:47 
AnswerRe: string trim and toupper/tolower Pin
Naveen4-Sep-08 20:10
Naveen4-Sep-08 20:10 
GeneralRe: string trim and toupper/tolower Pin
George_George4-Sep-08 20:11
George_George4-Sep-08 20:11 
AnswerRe: string trim and toupper/tolower Pin
Rajesh R Subramanian4-Sep-08 20:41
professionalRajesh R Subramanian4-Sep-08 20:41 
GeneralRe: string trim and toupper/tolower Pin
George_George4-Sep-08 21:39
George_George4-Sep-08 21:39 
QuestionClass initialization and memory allocation Pin
Christian Flutcher4-Sep-08 19:11
Christian Flutcher4-Sep-08 19:11 
AnswerRe: Class initialization and memory allocation [modified] Pin
_AnsHUMAN_ 4-Sep-08 19:27
_AnsHUMAN_ 4-Sep-08 19:27 
GeneralRe: Class initialization and memory allocation Pin
Christian Flutcher4-Sep-08 19:40
Christian Flutcher4-Sep-08 19:40 
GeneralRe: Class initialization and memory allocation Pin
Cedric Moonen4-Sep-08 19:57
Cedric Moonen4-Sep-08 19:57 
GeneralRe: Class initialization and memory allocation Pin
Christian Flutcher4-Sep-08 20:20
Christian Flutcher4-Sep-08 20:20 
GeneralRe: Class initialization and memory allocation Pin
Cedric Moonen4-Sep-08 20:27
Cedric Moonen4-Sep-08 20:27 
GeneralRe: Class initialization and memory allocation Pin
Christian Flutcher4-Sep-08 20:29
Christian Flutcher4-Sep-08 20:29 
GeneralRe: Class initialization and memory allocation Pin
_AnsHUMAN_ 4-Sep-08 19:58
_AnsHUMAN_ 4-Sep-08 19:58 
GeneralRe: Class initialization and memory allocation Pin
Rajesh R Subramanian4-Sep-08 20:16
professionalRajesh R Subramanian4-Sep-08 20:16 
GeneralRe: Class initialization and memory allocation Pin
_AnsHUMAN_ 4-Sep-08 20:29
_AnsHUMAN_ 4-Sep-08 20:29 
GeneralRe: Class initialization and memory allocation Pin
Naveen4-Sep-08 20:23
Naveen4-Sep-08 20:23 
GeneralRe: Class initialization and memory allocation Pin
Rajesh R Subramanian4-Sep-08 20:00
professionalRajesh R Subramanian4-Sep-08 20:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.