Changeset 10338 for trunk


Ignore:
Timestamp:
04/10/11 10:09:40 (13 years ago)
Author:
tbretz
Message:
Added Trim function.
Location:
trunk/FACT++/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/tools.cc

    r10183 r10338  
    4949    return str;
    5050}
     51
     52// --------------------------------------------------------------------------
     53//
     54//! This is a static helper to remove leading and trailing whitespaces.
     55//!
     56//! @param buf
     57//!    a pointer to the char array from which the whitespaces should be
     58//!    removed
     59//!
     60//! @returns
     61//!    a std::string with the whitespaces removed from buf
     62//
     63string Trim(const string &str)
     64{
     65    // Trim Both leading and trailing spaces
     66    const size_t start = str.find_first_not_of(' '); // Find the first character position after excluding leading blank spaces
     67    const size_t end   = str.find_last_not_of(' ');  // Find the first character position from reverse af
     68
     69    // if all spaces or empty return an empty string
     70    if (string::npos==start || string::npos==end)
     71        return string();
     72
     73    return str.substr(start, end-start+1);
     74}
  • trunk/FACT++/src/tools.h

    r10183 r10338  
    33std::string Format(const char *fmt, va_list &ap);
    44std::string Form(const char *fmt, ...);
     5std::string Trim(const std::string &str);
Note: See TracChangeset for help on using the changeset viewer.