Ignore:
Timestamp:
Apr 10, 2011, 10:09:40 AM (10 years ago)
Author:
tbretz
Message:
Added Trim function.
File:
1 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}
Note: See TracChangeset for help on using the changeset viewer.