[C + + standard library]_[primary]_[use template to delete strings before and after spaces ((w) string space)]

Source: Internet
Author: User


Scene:

1. C + + does not provide a function to remove the front and back spaces of the std::(W) string, such as Trimspace.

2. Many libraries are available, but in order to transplant the code is convenient, it is best to use the standard library to solve the standard library.


The bottom template implements the function of removing spaces. Test.cpp

#include <iostream> #include <stdlib.h> #include <string.h> #include <string> #include <  Ctype.h>using namespace STD;//1.VC implementation > 256 crashes. So you have to implement inline int IsSpace (int c) {    if (c = = 0x20 | | c = = 0x09 | | c== 0x0D)     {        return 1;    }     return 0;} Template<class t>t removepreandlastspace (const t& str) {        int length = Str.size ();    int i = 0,j = length-1;    while (i < length && IsSpace (Str[i])) {I ++;}     while (J >= 0 && IsSpace (str[j])) {j--;}     cout << i  << ":" << j<< endl;    if (j<i) return T (); &n bsp;   return Str.substr (i,j-i+1);} void Testwstring () {    wstring wstr;    wstring wres;    wstr = L "asdfasd 990 ";    wres = RemoVepreandlastspace (WSTR);    wcout << "wres:[" << wres  << "]" << Endl;     wstr = L "ASDFASD 990";    wres = Removepreandlastspace (wstr);     Wcout << "res:[" << wres  << "]" << endl;    wstr = L "ASDFASD 990";  &nbs P  wres = Removepreandlastspace (wstr);    wcout << "res:[" << wres  << "]" < < endl;    wstr = L "";    wres = Removepreandlastspace (wstr);    wcout << "res:[" << wres  << "]" << endl;    wstr = L "";    wres = Re Movepreandlastspace (WSTR);    wcout << "res:[" << wres  << "]" << Endl;     wstr = L ";  "  wres = Removepreandlastspace (wstr);    wcout < < "res:[" << wres  << "]" << Endl;} void TestString () {    string wstr;    string wres;    wstr = "ASDFASD 990" ;    wres = Removepreandlastspace (wstr);    cout << "wres:[" << wres  << "]" << endl;    wstr = "ASDFASD 990";    wres = Removepreandlastspace (WSTR) ;    cout << "res:[" << wres  << "]" << endl;    wstr = "Asdfas D 990 ";    wres = Removepreandlastspace (wstr);    cout <<" res:["<< wres& nbsp << "]" << endl;    wstr = "";    wres = Removepreandlastspace (WSTR);     cout << "res:[" << wres  << "]" << endl;    wstr = "";  &nbsp ;  wres = Removepreandlastspace (wstr);    cout << "res:[" << wres  << "]" <& Lt endl;    wstr = "+";    wres = Removepreandlastspace (wstr);    cout << "res:[" << wres  << "]" << Endl;} int main (int argc, char const *argv[]) {    testwstring ();    cout << " ... "<< endl;    teststring ();    return 0;...................

Output:

0:10WRES:[ASDFASD 990]0:10res:[asdfasd 990]1:11res:[asdfasd 990]0: -1res:[]1: -1res:[]0: -1res:[] ................................. 0:10WRES:[ASDFASD 990]0:10res:[asdfasd 990]1:11res:[asdfasd 990]0: -1res:[]1: -1res:[]0: -1res:[]



[C + + standard library]_[primary]_[use template to delete strings before and after spaces ((w) string space)]

Related Article

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.