C + + implementation removes whitespace characters before and after string strings

Source: Internet
Author: User

The string class provided by the C + + standard library does not provide a CString-like trim method, which is used to remove whitespace characters before and after a string. This functionality can be easily implemented using some of the methods of string itself.

As follows:

void Trim (String & str) {string blanks ("\f\v\r\t\n"); Str.erase (0,str.find_first_not_of (Blanks)); Str.erase ( Str.find_last_not_of (Blanks) + 1);}

The test is as follows:

int _tmain (int argc, _tchar* argv[]) {string S1 ("\ t   string  "), cout << "[" << S1 << "]" << E Ndl Trim (S1), cout << "after trim:\n", cout << "[" << S1 << "]" << Endl;return 0;}

C + + implementation removes whitespace characters before and after string strings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.