• <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>
            不錯不錯~~~,收下了。  回復  更多評論
              
            热久久这里只有精品| 久久久久久久综合日本亚洲| 久久人人爽人爽人人爽av| 久久久受www免费人成| 亚洲狠狠婷婷综合久久蜜芽| 99久久99久久| 亚洲色欲久久久久综合网| 久久超碰97人人做人人爱| 久久久99精品成人片中文字幕| 久久久WWW成人免费毛片| 性做久久久久久久| 国产精品亚洲美女久久久| 久久人人爽人人爽人人片av麻烦 | 99久久精品影院老鸭窝| 久久青青草原精品国产不卡| 久久久av波多野一区二区| 久久综合九色综合久99| 无码人妻久久一区二区三区免费丨 | 99精品国产在热久久无毒不卡 | 久久国产精品一区| 伊色综合久久之综合久久| 久久久人妻精品无码一区| 久久99精品久久久久久齐齐 | 国产一区二区久久久| 人人妻久久人人澡人人爽人人精品| 久久青青草原亚洲av无码| 久久久无码精品亚洲日韩京东传媒| 欧美成a人片免费看久久| 久久精品日日躁夜夜躁欧美 | 国产69精品久久久久99| 久久精品中文字幕一区| 一本一道久久综合狠狠老| 久久婷婷综合中文字幕| 亚洲国产婷婷香蕉久久久久久| 国内精品久久久久久久久电影网| 久久亚洲精品中文字幕三区| 日本精品久久久中文字幕| 久久国产精品99精品国产987| 日本精品久久久久中文字幕8| 亚洲人成伊人成综合网久久久 | 久久精品国产亚洲AV嫖农村妇女|