Monday, March 16, 2009

Strings in c++

I made the mistake of taking a c++ class.  Well itn's not a mistake it is good to be reminded of how much effort things are in c++.  Take strings for example. In c# if you want to lower case a string you do a str.ToLower() and it is lower case in c++ you have to do some thign like this.
   30     static void ToLower(string &itext)
   31     {
   32         transform(itext.begin(), itext.end(), itext.begin(), (int(*)(int)) tolower);
   33     }

This one wasn't all that bad it is still just one line. This what the first one I needed
for the project I am working on at the moment.  The other one I wished I had on several
was str.Trim() just to clear off the white space. So I spend 5 minuets googling and found
something simlar to this.  
 
   35 static void Trim(string &itext)
   36 {
   37     int startLoc = itext.find_first_not_of(" \t\n");
   38     int endLoc = itext.find_last_not_of(" \t\n");
   39     if((std::string::npos == startLoc) || (std::string::npos == endLoc))
   40     {
   41         itext = "";
   42     }
   43     else
   44     {
   45         itext = itext.substr(startLoc,endLoc - startLoc + 1);
   46     }
   47 }

it dosn't do any thing but white space but still it is better then a kick in the face.