trim is not part of the standard c/c++ library?

无人久伴 提交于 2019-12-20 17:37:59

问题


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. trim is not part of the standard c/c++ library?


回答1:


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.




回答2:


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 );
} 



回答3:


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;



回答4:


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.




回答5:


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 :-)




回答6:


I think the main reason is that ' ' is a rather narrow definition of whitespace; for example, I'd also want L' ' and L' ' removed by a trim() function; however, only the locale library has such insight.

The same rationale applies to the commonly-requested to_upper and to_lower methods -- there are some people (especially in Turkey) who disagree with the typical c &= ~0x20; mapping typically used for ASCII.




回答7:


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



来源:https://stackoverflow.com/questions/479080/trim-is-not-part-of-the-standard-c-c-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!