C++ 字串處理 重要函數

來源:互聯網
上載者:User

標籤:pre   記錄   trim   刪除字串   abc   ras   last   span   turn   

刪除字串首尾的空格:

string str1="abcdefg";
s.erase(0, s.find_first_not_of(" "));  /// 從頭開始找到第一個不為" "的位置; 刪除從a到b字串
str1.erase(s.find_last_not_of(" ") + 1);  /// 從last 開始 找到第一個不為" "的位置。 保留從0到n的字串

 

 記錄:

      find_first_not_of(“ ”)  // 從頭開始尋找第一個不為“ ”的位置, 返回的是位置;

      find_last_not_of(" ")  // 從末尾 開始尋找第一個不為“ ”的位置, 返回位置數;

      str1.erase(a,b) ; // 刪除 a到b-1的字串,保留其餘的 

      str1.erase(n);  // 保留從開頭開始的n個字串, 其中n是個數 不是位置;

 

PS:  別人寫的 trim()  

std::string& trim(std::string &s){    if (s.empty())    {        return s;    }    s.erase(0, s.find_first_not_of(" "));     s.erase(s.find_last_not_of(" ") + 1);    return s;}

 

C++ 字串處理 重要函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.