字符串替换:用参数字符数组成员替换字符串中的占位符(面试题)
来源:互联网 发布:东方卫视网络电视台 编辑:程序博客网 时间:2024/06/10 05:39
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;}
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: 14second 'needle' found at: 44'haystack' also found at: 30Period found at: 51There 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;}
阅读全文
1 0
- 字符串替换:用参数字符数组成员替换字符串中的占位符(面试题)
- (程序员面试题)字符串处理之字符替换
- 面试题4:替换字符串中的空格
- JAVA字符串占位符替换
- JAVA字符串占位符替换
- JAVA字符串占位符替换
- JAVA字符串占位符替换
- 腾讯面试题 替换字符串
- 面试题 -- 字符串替换空格
- 华为面试题----字符串替换
- 替换字符串中的特殊字符
- 替换字符串中的特殊字符
- 字符串中的部分字符替换
- replace替换字符串中的字符
- 剑指offer面试题4:替换字符串中的空格
- 【面试题】剑指offer04--替换字符串中的空格
- 剑指Offer算法题之字符串替换字符--面试题4:替换空格
- JavaScript中的字符串,数组,替换
- java中的TreeMap类使用实例解析
- 监督学习之K近邻算法(KNN)
- 不使用判断比较符比较两个整数的大小(位运算)
- [科研笔记] 关于人工智能与算法项目的思考
- linux下zookeeper的安装步骤
- 字符串替换:用参数字符数组成员替换字符串中的占位符(面试题)
- 堆排序
- Eclipse怎样连接并打开oracle等数据库?
- select编程
- class path resource cannot be opened because it does not exits
- struts2上传文件时,关于在action里面获取上传文件的文件名
- Windows下的Python安装pip,及使用技巧
- linux-桥接与网卡聚合
- 动态规划解N个矩阵链乘问题