views:

1688

answers:

6

Is it me or are there no standard trim functions in the c or c++ library? is there any single function that acts as a trim? If not can anyone tell me Why trim is not part of the standard library? (i know trim is in boost)

My trim code is

std::string trim(const std::string &str)
{
    size_t s = str.find_first_not_of(" \n\r\t");
    size_t e = str.find_last_not_of (" \n\r\t");

    if(( string::npos == s) || ( string::npos == e))
        return "";
    else
        return str.substr(s, e-s+1);
}

test: cout << trim(" \n\r\r\n \r\n text here\nwith return \n\r\r\n \r\n "); -edit- i mostly wanted to know why it wasnt in the standard library, BobbyShaftoe answer is great. http://stackoverflow.com/questions/479080/trim-is-not-part-of-the-standard-c-c-library#479091

+11  A: 

No, you have to write it yourself or use some other library like Boost and so forth.

In C++, you could do:

#include <string>

const std::string whiteSpaces( " \f\n\r\t\v" );


void trimRight( std::string& str,
      const std::string& trimChars = whiteSpaces )
{
   std::string::size_type pos = str.find_last_not_of( trimChars );
   str.erase( pos + 1 );    
}


void trimLeft( std::string& str,
      const std::string& trimChars = whiteSpaces )
{
   std::string::size_type pos = str.find_first_not_of( trimChars );
   str.erase( 0, pos );
}


void trim( std::string& str, const std::string& trimChars = whiteSpaces )
{
   trimRight( str, trimChars );
   trimLeft( str, trimChars );
}
BobbyShaftoe
+1  A: 

I don't know too much about C++, but check this out to see how to accomplish this in C. (In my defense: you did say C/C++ in the title :-)

Anthony Cuozzo
+1  A: 

In the old days, with scanf(), one space would match any number of whitespace characters.

This is why we used fgets() followed by sscanf(). To prevent matching a newline as part of that whitespace.

Mr.Ree
+6  A: 

The reason trim() isn't in the standard library is that when the last standard was made, they had to strike a balance between formalizing current behavior (adding nothing new, just stabilizing what already existed), and adding new functionality. In general, they preferred not to add a feature unless it either 1) would be impossible otherwise, or 2) there were significant drawbacks to using third-party libraries instead. Making too many changes would

  • break compatibility with existing code (which may have defined its own trim()
  • Add more work for compiler writers (who already had a huge amount of work ahead of them)
  • Make the language more complex to learn and use.

With trim(), there are no major interoperability issues. As long as your third-party trim() implementation takes a string and returns a string, we don't really care where it's defined. So it's not really necessary In the standard library. It can be easily supplied by other libraries.

By contrast, something like the string class or vector, are classes that the standard library must supply, because if you use a custom string class, only string operations from that library will work. With a standard library string, third-party libraries can target this common string definition, and everyone wins.

When the last standard came out, Herb Sutter wrote a post describing this very well here

Of course, it would be nice to have a trim() function, but they had bigger fish to fry. They had to standardize all the basics first. I don't think C++0x will add a trim function, but it will add a lot of other convenience utilities that back in '98 were considered "unnecessary" or too specialized.

jalf
+1  A: 

As others have said, the committee was busy with (many) other issues. Boost has filled some of the gaps, like with the string_algo library's trimming functions:

string testTrim(" \n\r\r\n \r\n text here\nwith return \n\r\r\n \r\n ");
trim(testTrim);
cout << testTrim;
MattyT
A: 

You can use VC++. CString class has a trim function. :D

Owen