• <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)

            隨筆分類(lèi)

            隨筆檔案

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            自己寫(xiě)了一個(gè):
            /***********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***********/

            目前個(gè)人還沒(méi)發(fā)現(xiàn)C,C++中有提供像Trim()那樣的方法,
            如有更好方法,還請(qǐng)各位大哥指點(diǎn)。

            posted on 2007-11-04 15:43 希冀0714 閱讀(7604) 評(píng)論(5)  編輯 收藏 引用 所屬分類(lèi): 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;
            }
              回復(fù)  更多評(píng)論
              
            # 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;
            }
              回復(fù)  更多評(píng)論
              
            # re: 求C++中:去掉字符串首尾空格的方法. 2007-11-04 20:50 希冀0714
            謝謝,這樣的程序看起來(lái)清晰多了,學(xué)習(xí)中.  回復(fù)  更多評(píng)論
              
            # re: 求C++中:去掉字符串首尾空格的方法. 2007-11-05 08:44 <a href=http://minidx.com>minidxer</a>
            不錯(cuò)不錯(cuò)~~~,收下了。  回復(fù)  更多評(píng)論
              
            伊人久久精品无码二区麻豆| av无码久久久久久不卡网站 | 久久精品国产精品国产精品污| 91精品国产91久久综合| 人人狠狠综合久久亚洲婷婷| 无码人妻少妇久久中文字幕| 99久久精品日本一区二区免费 | 精品久久久久久久中文字幕| 亚洲精品无码久久毛片| 精品久久久久久| 亚洲精品无码专区久久久| 久久精品国产一区二区电影| 日产精品久久久久久久性色| 久久99热这里只有精品国产| 日韩精品久久无码人妻中文字幕| 精品国产综合区久久久久久| 色欲av伊人久久大香线蕉影院| 久久久这里有精品中文字幕| 精品亚洲综合久久中文字幕| 无码精品久久久久久人妻中字| 久久九九久精品国产免费直播| A狠狠久久蜜臀婷色中文网| 欧美黑人激情性久久| 日韩AV毛片精品久久久| 99热精品久久只有精品| 国产亚洲婷婷香蕉久久精品| 久久人妻少妇嫩草AV无码专区| 久久精品国产99久久久古代| 一级做a爰片久久毛片看看| 欧美色综合久久久久久| 国产精品九九久久免费视频| yellow中文字幕久久网| 韩国三级中文字幕hd久久精品| 93精91精品国产综合久久香蕉| 高清免费久久午夜精品| 久久精品国产福利国产秒| 久久99精品国产麻豆| 99久久er这里只有精品18| 99久久免费国产特黄| 色综合久久88色综合天天| 国产A级毛片久久久精品毛片|