• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 4  文章 - 4  trackbacks - 0
            <2007年11月>
            28293031123
            45678910
            11121314151617
            18192021222324
            2526272829301
            2345678

            常用鏈接

            留言簿(2)

            隨筆分類

            隨筆檔案

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            自己寫了一個:
            /***********begin test file***********/
            #include <iostream>
            #include <string>

            int main()
            {
             std::string str1 = "   hello world!   ";
             std::string trimstring = " ";
             std::cout << "str = \"" << str1 << "\"" << std::endl;
             std::cout << "str.find_first_of(' ')     : " << str1.find_first_of(trimstring)     << std::endl;
             std::cout << "str.find_first_not_of(' ') : " << str1.find_first_not_of(trimstring) << std::endl;
             std::cout << "str.find_last_of(' ')     : " << str1.find_last_of(trimstring)      << std::endl;
             std::cout << "str.find_last_not_of(' ')  : " << str1.find_last_not_of(trimstring)  << std::endl;
             str1.erase(str1.find_last_not_of(trimstring)+1);
             std::cout << "after right trim : \"" << str1 << "\"" << std::endl;
             str1.erase(0,str1.find_first_not_of(trimstring));
             std::cout << "after left trim  : \"" << str1 << "\"" << std::endl;
             return 0;
            }
            /***********end test file***********/

            目前個人還沒發現C,C++中有提供像Trim()那樣的方法,
            如有更好方法,還請各位大哥指點。

            posted on 2007-11-04 15:43 希冀0714 閱讀(7594) 評論(5)  編輯 收藏 引用 所屬分類: C++

            FeedBack:
            # re: 求C++中:去掉字符串首尾空格的方法. 2007-11-04 19:10 lovedday
            #include <string.h>
            #include <stdio.h>

            #pragma warning(disable : 4996)

            char* rtrim(char* s)
            {
            if(s == NULL)
            return NULL;

            // set tail white space as '\0'
            for(size_t i = strlen(s)-1; i >= 0 && s[i] == ' '; i--)
            s[i] = '\0';

            return s;
            }

            char* ltrim(char* s)
            {
            if(s == NULL)
            return NULL;

            char* p;

            // skip head white space
            for(p = s; *p == ' '; p++)
            ;

            if(p != s) // if address p is not same as address s
            {
            size_t length = strlen(p);

            if(length > 0) // so p is not null
            {
            memmove(s, p, length);
            s[length] = '\0';
            }
            }

            return s;
            }

            char* trim(char* s)
            {
            if(s == NULL)
            return NULL;

            rtrim(s);

            return ltrim(s);
            }

            int main()
            {
            char s[10];
            strcpy(s, " ab cde ");

            trim(s);

            printf("after trim:\n");

            if(*s != '\0')
            printf("%s, length = %d\n", s, strlen(s));
            else
            printf("s is null\n");

            return 0;
            }
              回復  更多評論
              
            # re: 求C++中:去掉字符串首尾空格的方法. 2007-11-04 19:44 lovedday
            #include <iostream>
            #include <string>

            using namespace std;

            void trim(string& str)
            {
            str.erase(str.find_last_not_of(' ')+1, string::npos);
            str.erase(0, str.find_first_not_of(' '));
            }

            int main()
            {
            string str = " hello world! ";

            trim(str);

            cout << "after trim:" << endl;
            cout << str << endl;

            return 0;
            }
              回復  更多評論
              
            # re: 求C++中:去掉字符串首尾空格的方法. 2007-11-04 20:50 希冀0714
            謝謝,這樣的程序看起來清晰多了,學習中.  回復  更多評論
              
            # re: 求C++中:去掉字符串首尾空格的方法. 2007-11-05 08:44 <a href=http://minidx.com>minidxer</a>
            不錯不錯~~~,收下了。  回復  更多評論
              
            无码任你躁久久久久久| 国产亚洲精品美女久久久| 国产精品久久久久久久久久免费| 国产成人精品久久免费动漫| 日本免费久久久久久久网站| 亚洲国产高清精品线久久| 久久WWW免费人成一看片| 99久久99这里只有免费的精品| 91精品观看91久久久久久| 亚洲一区精品伊人久久伊人| 无码国内精品久久人妻| 久久精品亚洲男人的天堂| 国产激情久久久久久熟女老人| 国内精品伊人久久久久| 四虎亚洲国产成人久久精品| av午夜福利一片免费看久久| 亚洲日本va午夜中文字幕久久| 91精品国产综合久久精品| 欧美精品丝袜久久久中文字幕 | 久久国产高潮流白浆免费观看| 国产精品嫩草影院久久| 亚洲精品乱码久久久久久蜜桃不卡 | 午夜视频久久久久一区 | 久久久久久亚洲精品无码| 久久人人爽爽爽人久久久| 久久久久久亚洲精品无码| 91精品国产色综久久| 国产亚洲婷婷香蕉久久精品| 狠狠色丁香久久婷婷综合| 性做久久久久久免费观看| 国产巨作麻豆欧美亚洲综合久久| 国内精品伊人久久久久AV影院| 一级做a爰片久久毛片毛片| 久久久久无码专区亚洲av| 777米奇久久最新地址| 久久不见久久见免费视频7| 婷婷综合久久中文字幕蜜桃三电影| 狠狠色综合网站久久久久久久| 久久精品国产69国产精品亚洲| 精品无码久久久久久尤物| 久久久久亚洲AV无码网站|