c语言void replace(str,ch)替换字符串,C++ 中字符串查找、字符串截取、字符串替换...
1、字符串查找
s.find(s1) //查找s中第一次出現s1的位置,并返回(包括0)
s.rfind(s1) //查找s中最后次出現s1的位置,并返回(包括0)
s.find_first_of(s1) //查找在s1中任意一個字符在s中第一次出現的位置,并返回(包括0)
s.find_last_of(s1) //查找在s1中任意一個字符在s中最后一次出現的位置,并返回(包括0)
s.fin_first_not_of(s1) //查找s中第一個不屬于s1中的字符的位置,并返回(包括0)
s.fin_last_not_of(s1) //查找s中最后一個不屬于s1中的字符的位置,并返回(包括0)
2、字符串截取
s.substr(pos, n) //截取s中從pos開始(包括0)的n個字符的子串,并返回
s.substr(pos) //截取s中從從pos開始(包括0)到末尾的所有字符的子串,并返回
3、字符串替換
s.replace(pos, n, s1) //用s1替換s中從pos開始(包括0)的n個字符的子串
4、代碼測試(字符串操作.cpp)
#include
using namespace std;
/* 字符串查找 */
void findSubString(string str){
// find()函數的使用,返回查找對象第一次出現的位置.
cout << str.find("fs") << endl;
// rfind()函數的使用,返回查找對象最后出現的位置
cout << str.rfind("s") << endl;
}
/* 字符串截取 */
void getSubString(string str){
// substr(pos)函數的使用,返回從pos開始(包含pos位置的字符)所有的字符
cout << str.substr(2) << endl;
// substr(pos,n),返回從pos開始(包含pos位置的字符)n個字符
cout << str.substr(2, 2) << endl;
}
/* 字符串替換 */
void replaceString(string str){
// replace(pos,n,s1),用s1替換從pos開始的n個字符
cout << str.replace(0,2,"xiaoming") << endl;
}
int main()
{
string str = string("sdfsf");
// findSubString(str);
// getSubString(str);
replaceString(str);
return 0;
}
5、字符替換(用x替換字符串中所有的a.cpp)
#include
using namespace std;
/* 用x替換a */
void replaceAWithX(string str){
int pos;
pos = str.find("a");
while(pos != -1){
// str.length()求字符的長度,注意str必須是string類型
str.replace(pos,string("a").length(),"x");
pos = str.find("a");
}
cout << str << endl;
}
int main()
{
string str = string("fsafsdf");
replaceAWithX(str);
return 0;
}
ios 字符串處理:截取字符串、匹配字符串、分隔字符串
1.截取字符串 NSString*string =@"sdfsfsfsAdfsdf";string = [string?substringToIndex:7];//截取掉下標7之后 ...
字符串函數(strcpy字符串拷,strcmp字符串比較,strstr字符串查找,strDelChar字符串刪除字符,strrev字符串反序,memmove拷貝內存塊,strlen字符串長度)
1.strcpy字符串拷貝拷貝pStrSource到pStrDest,并返回pStrDest地址(源和目標位置重疊情況除外) char *strcpy(char *pStrDest, const ch ...
js獲取(包括中文)字符串長度與截取字符串
/** * @param begin 截取開始的索引 * @param num 截取的長度 */ //截取字符串(包括中文) function SetString(str, len) { var st ...
js常用方法和檢查是否有特殊字符串和倒序截取字符串
js常用方法demo Binding RelativeSource={RelativeSource Self},Path=Text}" Text=&quo ...
POJ 1160 經典區間dp/四邊形優化
鏈接http://poj.org/problem?id=1160 很好的一個題,涉及到了以前老師說過的一個題目,可惜沒往那上面想. 題意,給出N個城鎮的地址,他們在一條直線上,現在要選擇P個城鎮建立郵 ...
總結
以上是生活随笔為你收集整理的c语言void replace(str,ch)替换字符串,C++ 中字符串查找、字符串截取、字符串替换...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux ping结果中mdev,Li
- 下一篇: 华中科技大学c语言作业答案,华中科技大学