![]() |
![]() |
|||||
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
|
Q: Convert an AnsiString to all lower case or upper caseAnswerThe AnsiString class provides two member functions called UpperCase and LowerCase that will accomplish what you need. UpperCase and LowerCase are prototyped in the file DSTRING.H. The prototypes look like this: AnsiString __fastcall LowerCase() const; AnsiString __fastcall UpperCase() const; Notice that the functions are declared as const methods of AnsiString. This allows you to call UpperCase or LowerCase on a constant AnsiString object. The const declaration means that UpperCase and LowerCase won't modify the current AnsiString object. This has an important consequence: LowerCase and UpperCase return a new AnsiString object that contains the converted string from the source object. The original AnsiString object is not changed. Here is a code example that shows how to use the functions: // Example of how to correctly use UpperCase and LowerCase AnsiString str = "Hello Dr. Crane"; AnsiString upper = str.UpperCase(); // result= HELLO DR. CRANE AnsiString lower = str.LowerCase(); // result= hello dr. crane // Example of code that does nothing AnsiString str = "Hello Dr. Crane"; str.UpperCase(); // code has no effect, str is not modifed by UpperCase. Note: The UpperCase and LowerCase methods of AnsiString work by calling two global functions called AnsiUpperCase and AnsiLowerCase in SYSUTILS.PAS. You can use the global functions if you prefer. Note: I once needed to test a command line parameter as part of a programming assignment. I used the code below to provide a case insensitive way of testing the command line item. if( (ParamCount() >= 1) && (ParamStr(1).UpperCase() == "-ISX")) { // do some cool stuff. } | ||||||
All rights reserved. |