• <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>
            不錯不錯~~~,收下了。  回復  更多評論
              
            91精品婷婷国产综合久久| 中文字幕无码久久人妻| 久久人人爽人人爽人人爽| 久久婷婷五月综合成人D啪 | 日韩精品国产自在久久现线拍| 久久久久久久波多野结衣高潮| 99久久免费国产精品| 亚洲综合日韩久久成人AV| 99久久综合国产精品免费| 亚洲午夜无码久久久久小说| 大香伊人久久精品一区二区| 精品久久国产一区二区三区香蕉| 一本一道久久综合狠狠老| 亚洲精品无码专区久久久 | 久久99中文字幕久久| 久久不见久久见免费视频7| 嫩草伊人久久精品少妇AV| 亚洲AV日韩精品久久久久| 久久精品国产精品亚洲精品| 亚洲国产精品热久久| 欧美精品一区二区久久| 伊人久久精品无码二区麻豆| 久久婷婷五月综合成人D啪| 精品国产VA久久久久久久冰 | 天天综合久久久网| 性高朝久久久久久久久久| 久久久久无码精品国产不卡| 中文字幕亚洲综合久久2| 久久综合给合久久狠狠狠97色| 久久不见久久见免费影院www日本| 色婷婷综合久久久中文字幕| 浪潮AV色综合久久天堂| 国产成人久久精品激情 | 亚洲国产成人精品久久久国产成人一区二区三区综 | 性欧美丰满熟妇XXXX性久久久 | 狠狠干狠狠久久| 久久人人爽人人爽人人AV| 久久国产精品-国产精品| 日韩精品无码久久久久久| 亚洲精品无码久久久久| 国产精品9999久久久久|