Generated on Mon Jun 23 16:24:56 2008 for BIU-1.7.0 by doxygen 1.5.1

src/biu/util/Util_String.cpp

Go to the documentation of this file.
00001 // Util_String.cpp: Implementierung der Klasse Util_String.
00002 /*
00003 
00004     <author>    Martin Mann                 </author>
00005     <created>   9.10.2005                   </created>
00006 
00007     <info>                                  </info>
00008 */
00009 //
00011 
00012 #include "biu/util/Util_String.h"
00013 
00014 
00015 namespace biu {
00016   namespace util {
00017 
00019 // wandelt int in string um
00021 std::string Util_String::int2str(const int& number) {
00022     std::ostringstream oss;
00023     oss << number;
00024     return oss.str();
00025 }
00026 
00028 // wandelt integer string in int um
00030 int Util_String::str2int(const std::string& numString) {
00031     if (numString.empty())
00032         return 0;
00033     std::istringstream iss(numString);
00034     int retInt=0;
00035     iss >> retInt;
00036     return retInt;
00037 }
00038 
00039 
00041 // liefert anzahl der vorkommen von <c> in <str>
00043 int Util_String::countChar( const std::string &str, const char c) {
00044     std::string::size_type  i=0;
00045     int ret = 0;
00046     for ( ;i<str.size();i++) 
00047         if (str[i] == c) 
00048             ret++;
00049     return ret;
00050 }
00051 
00052 
00054 // liefert laenge der laengsten wiederholung von <c> in <str>
00056 int Util_String::maxSubseq( const std::string &str, const char c) {
00057     int m=0, act=0;
00058     std::string::size_type i=str.find_first_not_of(c);
00059     for(;i<str.find_last_not_of(c);i++) {
00060 //      std::cerr <<i <<" = " <<str[i] ;
00061         if (str[i]==c)
00062             act++;
00063         else 
00064             act=0;
00065         if (act > m) m = act;
00066 //      std::cerr <<" act = " <<act <<" max = " << m <<"\n";
00067     }
00068     return m;
00069 }
00070 
00071 
00073 // konvertiert den string in grossbuchstaben
00075 std::string Util_String::str2upperCase(const std::string &str) {
00076     std::string ret(str);
00077     char diff = 'A'-'a';
00078     for (std::string::size_type i=0; i<ret.size(); i++)
00079         if (ret[i]>96 && ret[i]<123)
00080             ret[i] = ret[i]+diff;
00081     return ret;
00082 }
00083 
00084 
00085 
00087 // prueft ob string nur aus elementen des alphabets besteht
00089 bool Util_String::isAlphStr(const std::string str, const std::string alph) {
00090     return str.find_first_not_of(alph,0) < str.size();
00091 }
00092 
00094 // entfernt fuehrende und endende "\n" und " " und "\t"
00096 std::string Util_String::chompStr(const std::string &str) {
00097     std::string ret(str);
00098         // remove leading whitespaces
00099     ret.erase(0,ret.find_first_not_of("\n \t\r"));
00100         // remove tailing whitespaces
00101     ret.erase(ret.find_last_not_of("\n \t\r")+1);
00102     return ret;
00103 }
00104 
00105 
00107 // inverts the string
00109 std::string Util_String::invert(const std::string& str) {
00110     std::string ret(str);
00111     std::string::size_type i = 0;
00112     for (std::string::const_reverse_iterator it = str.rbegin(); it!= str.rend(); it++)
00113         ret[i++] = *it;
00114     return ret;
00115 }
00116 
00117   } //namespace util
00118 } // namespace biu