Re: [問題] 字串處理

看板C_and_CPP作者 (pziyout)時間14年前 (2010/04/12 08:13), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/3 (看更多)
※ 引述《yu1 (~renard~)》之銘言: : 遇到的問題: (題意請描述清楚) : 截取A●B●C◎D●E 的字串 : 其中●和◎是長度及內容都會改變的字串 : ABCDE 是長度及內容都不變的字串 : 希望得到的正確結果: : 只希望留◎ : 開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux) : VC++ : 補充說明: : 這是網頁上截取的字串 : 一時想不到有什麼辦法處理 : 想問大家有沒有什麼好的方法呢 : 謝謝! // 在 str 字串中擷取介於 t1 與 t2 之間的文字 string grepstr( const string& str , const string& t1 , const string& t2 ) { return str.substr(str.find(t1)+t1.size(), str.find(t2)-str.find(t1)-t1.size()) ; } int main() { string b1str = "<ABC>" ; string e1str = "</ABC>" ; string b2str = "<DE>" ; string e2str = "</DE>" ; string in1 = "This is s book." ; string in2 = "This is s horse." ; string s = b1str + in1 + e1str + b2str + in2 + e2str ; cout << grepstr(s,b1str,e1str) << endl ; cout << grepstr(s,b2str,e2str) << endl ; return 0 ; } // 輸出: This is s book. This is s horse. -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.115.25.24
文章代碼(AID): #1BmcMaD0 (C_and_CPP)
討論串 (同標題文章)
本文引述了以下文章的的內容:
問題
0
1
完整討論串 (本文為第 2 之 3 篇):
問題
0
1
問題
0
6
文章代碼(AID): #1BmcMaD0 (C_and_CPP)