字符串替换:用参数字符数组成员替换字符串中的占位符(面试题)
生活随笔
收集整理的這篇文章主要介紹了
字符串替换:用参数字符数组成员替换字符串中的占位符(面试题)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、問題描述:
請你實現一個簡單的字符串替換函數。原串中需要替換的占位符為"%s",請按照參數列表的順序一一替換占位符。若參數列表的字符數大于占位符個數。
則將剩下的參數字符添加到字符串的結尾。給定一個字符串A,同時給定它的長度n及參數字符數組arg和它的大小m,請返回替換后的字符串。保證參
數個數大于等于占位符個數。保證原串由大小寫英文字母組成,同時長度小于等于500。
測試樣例:
"A%sC%sE",7,['B','D','F']
返回:"ABCDEF"
2、代碼實現
方法一:
#include <iostream> #include <string> #include <stdlib.h> #include <vector>using namespace std;class StringFormat { public:string formatString(const string& str, int n, vector<char> arg, int m){string _str = str; string ret;if(str.empty() == true || arg.empty() == true)return ret;int i = 0;while(_str.find("%s") != string::npos){ret += _str.substr( 0, _str.find( "%s" ) ) ; ret += arg[i++] ; _str = _str.substr( _str.find( "%s" ) + 2 ) ;//把%后沒有替換的部分賦值后繼續}ret += _str;//加上最后不需要替換的部分while(i < m){ret += arg[i++];}return ret;} };void test() {StringFormat sf;const string str("A%sC%sE");char arr[] = {'B','D','F'};vector<char> arg;int m = sizeof(arr)/sizeof(arr[0]);for(int i = 0; i < m; i++)arg.push_back(arr[i]);string ret = sf.formatString(str, str.length(), arg, m);cout<<ret<<endl; }
3、考查知識:string.find() 和 string.substr() 的使用
string中 find()的應用? (rfind()?類似,只是從反向查找) 原型如下: (1)size_t find (const string& str, size_t pos =?0) const;? //查找對象--string類對象 (2)size_t find (const char* s, size_t pos = 0) const; //查找對象--字符串 (3)size_t find (const char* s, size_t pos, size_t n) const;? //查找對象--字符串的前n個字符 (4)size_t find (char c, size_t pos = 0) const;? //查找對象--字符 結果:找到 -- 返回 第一個字符的索引 ?????沒找到--返回?? string::npos ? 示例:
int main () { std::string str ("There are two needles in this haystack with needles."); std::string str2 ("needle"); // different member versions of find in the same order as above: std::size_t found = str.find(str2); if (found!=std::string::npos) std::cout << "first 'needle' found at: " << found << '\n'; found=str.find("needles are small",found+1,6); if (found!=std::string::npos) std::cout << "second 'needle' found at: " << found << '\n'; found=str.find("haystack"); if (found!=std::string::npos) std::cout << "'haystack' also found at: " << found << '\n'; found=str.find('.'); if (found!=std::string::npos) std::cout << "Period found at: " << found << '\n'; // let's replace the first needle: str.replace(str.find(str2),str2.length(),"preposition"); //replace 用法 std::cout << str << '\n'; return 0; }
結果:
first 'needle' found at: 14 second 'needle' found at: 44 'haystack' also found at: 30 Period found at: 51 There are two prepositions in this haystack with needles
其他還有? find_first_of(), find_last_of(), find_first_not_of(), find_last_not_of() 作用是查找?? 字符串中?任意一個滿足的查找條件的字符 string snake1("cobra"); int where = snake1.find_first_of("hark"); 返回3? 因為 "hark"中 的一個字符 在 snake1--cobra 中第一次出現的是? 字符'r'(3為 cobra 中'r'的索引) 同理: int where = snake1.find_last_of("hark"); 返回4??因為 "hark"中 的一個字符 在 snake1--cobra 中最后一次出現的是? 字符'a'(3為 cobra 中'r'的索引)
方法二:
string FormatString(const string& str, int n, const vector<char>& arg, int m) {string formatstr;formatstr.reserve(str.size());int pos = 0;for(int i = 0; i < n; ++i){if(str[i] == '%' && str[i+1] == 's' && i+1 < n)//替換{assert(pos < m);formatstr.push_back(arg[pos++]);++i;}else//保存%s 之前的字符內容{formatstr.push_back(str[i]);}}while(pos < m)formatstr.push_back(arg[pos++]);return formatstr; }
總結
以上是生活随笔為你收集整理的字符串替换:用参数字符数组成员替换字符串中的占位符(面试题)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python中文分词神器---jieba
- 下一篇: 如何用css和HTML结合画熊,结合伪元