• <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 閱讀(7604) 評論(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>
            不錯不錯~~~,收下了。  回復  更多評論
              
            久久免费国产精品| 99精品国产综合久久久久五月天| 蜜臀久久99精品久久久久久小说| 日日噜噜夜夜狠狠久久丁香五月| 精品久久久久久中文字幕人妻最新| 精品久久久久中文字幕日本| 国产精品久久久99| 亚洲熟妇无码另类久久久| 2021精品国产综合久久| 久久这里的只有是精品23| 999久久久免费精品国产| 亚洲欧洲久久久精品| 潮喷大喷水系列无码久久精品| 国产AV影片久久久久久| 日韩久久久久久中文人妻| 久久久久亚洲精品无码网址| 久久亚洲精精品中文字幕| 亚洲伊人久久综合影院| 久久亚洲国产精品一区二区| 影音先锋女人AV鲁色资源网久久 | 99久久免费只有精品国产| 久久只有这精品99| 久久久国产精华液| 国产精品久久久久久久久鸭| 久久这里的只有是精品23| 久久九九免费高清视频| 品成人欧美大片久久国产欧美... 品成人欧美大片久久国产欧美 | 久久国产精品成人免费| 精品国产99久久久久久麻豆| 久久成人永久免费播放| 丰满少妇高潮惨叫久久久| 亚洲精品国产美女久久久 | 一本久久a久久精品亚洲| 久久久亚洲精品蜜桃臀| 青青青伊人色综合久久| 粉嫩小泬无遮挡久久久久久| 精品久久久久久中文字幕人妻最新| 久久精品aⅴ无码中文字字幕不卡| 亚洲国产成人久久综合一区77| 久久性生大片免费观看性| 人妻丰满?V无码久久不卡|