• <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
            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            常用鏈接

            留言簿(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>
            不錯不錯~~~,收下了。  回復  更多評論
              
            久久亚洲中文字幕精品一区四| 国产精品久久久久久久久鸭| 久久久久九国产精品| 久久国产成人亚洲精品影院| 久久亚洲av无码精品浪潮| 久久久午夜精品| 日本强好片久久久久久AAA | 国产精品久久久久久影院| 国产精品亚洲综合专区片高清久久久 | 精品综合久久久久久97| 久久精品成人免费网站| 伊人久久大香线蕉无码麻豆| 国内精品久久人妻互换| 久久久久久国产a免费观看黄色大片| 久久香蕉国产线看观看精品yw| 精品久久人人做人人爽综合| 无码人妻少妇久久中文字幕蜜桃| 久久精品国产欧美日韩| 国产精品久久久久久影院| 国产精品久久久久久久久软件 | 久久免费香蕉视频| 狠狠色婷婷综合天天久久丁香| 久久久久久久久波多野高潮| 久久国产精品免费一区二区三区| 国内精品综合久久久40p| 欧美久久一级内射wwwwww.| 国产精品一久久香蕉产线看| 久久综合给久久狠狠97色| 久久婷婷五月综合色99啪ak| 久久er国产精品免费观看2| 欧美黑人激情性久久| 久久免费视频1| 久久久SS麻豆欧美国产日韩| 热99RE久久精品这里都是精品免费| 国产69精品久久久久9999| 久久福利青草精品资源站| 久久99精品国产自在现线小黄鸭| 久久精品卫校国产小美女| 一本色道久久99一综合| 蜜臀久久99精品久久久久久小说 | 久久WWW免费人成一看片|