#include <iostream> #include <string> using namespace std; int main() { string str; cin>>str; //主要字符串是从0开始计数的 cout<<"ab在str中的位置:"<<str.find("ab")<<endl; //返回第一次出现ab的位置,没有则返回一串乱码 cout<<"ab在str[2]到str[n-1]中的位置:"<<str.find("ab",2)<<endl; //返回第一次从str[2]开始出现ab的位置,没有则返回一串乱码 cout<<"ab在str[0]到str[2]中的位置:"<<str.rfind("ab",2)<<endl; //返回ab在str[0]到str[2]中的位置,没有则返回一串乱码 return 0; }
输入1ios
sdefdwefdadabbababab
输出1数组
ab在str中的位置:11 ab在str[2]到str[n-1]中的位置:11 ab在str[0]到str[2]中的位置:18446744073709551615 Program ended with exit code: 0
输入2app
abfeofihwabab
输出2ide
ab在str中的位置:0 ab在str[2]到str[n-1]中的位置:9 ab在str[0]到str[2]中的位置:0 Program ended with exit code: 0
输入3函数
asdfghjk
输出3ui
ab在str中的位置:18446744073709551615 ab在str[2]到str[n-1]中的位置:18446744073709551615 ab在str[0]到str[2]中的位置:18446744073709551615 Program ended with exit code: 0
#include <iostream> #include <string> using namespace std; int main() { string str; cin>>str; cout<<"返回str[3]及其之后的子串:"<<str.substr(3)<<endl; //若小于限制长度则报错 cout<<"从ste[2]开始由四个字符组成的子串:"<<str.substr(2,4)<<endl; //若小于限制长度则报错 return 0; }
输入1this
asdfghjkl;'/.,mnbvcxz
输出1spa
返回str[3]及其之后的子串:fghjkl;'/.,mnbvcxz 从ste[2]开始由四个字符组成的子串:dfgh Program ended with exit code: 0
示列1指针
#include <iostream> #include <string> using namespace std; int main() { string line = "this@ is@ a test string!"; line = line.replace(line.find("@"), 1, ""); //从第一个@位置替换第一个@为空 cout<<line<<endl; return 0; }
输出code
his is@ a test string! Program ended with exit code: 0
示列2
#include <iostream> #include <string> using namespace std; int main() { string line = "this@ is@ a test string!"; line = line.replace(line.begin(), line.begin()+6, ""); //用str替换从begin位置开始的6个字符 cout << line << endl; return 0; }
示列3
#include <iostream> #include <string> using namespace std; int main() { string line = "this@ is@ a test string!"; char* str = "12345"; line = line.replace(0, 5, str); //用str替换从指定位置0开始长度为5的字符串 cout << line << endl; return 0; }
输出
12345 is@ a test string! Program ended with exit code: 0
#include <string> #include <iostream> using namespace std; int main() { string str; cin>>str; cout<<"从2号位置插入字符串jkl并造成新的字符串返回:"<<str.insert(2, "jkl")<<endl; return 0; }
输入
sdfgh
输出
从2号位置插入字符串jkl并造成新的字符串返回:sdjklfgh Program ended with exit code: 0
#include <iostream> #include <string> using namespace std; int main() { string str; cin>>str; cout<<"在字符串str后面添加字符串ABC:"<<str.append("ABC")<<endl; return 0; }
输入
diguwhdcow
输出
在字符串str后面添加字符串ABC:diguwhdcowABC Program ended with exit code: 0
#include <iostream> #include <string> using namespace std; int main() { string str1,str2; cin>>str1>>str2; cout<<"str1:"<<str1<<endl; cout<<"str2:"<<str2<<endl; swap(str1, str2); cout<<"str1:"<<str1<<endl; cout<<"str2:"<<str2<<endl; }
输入
qwertyui asdfghjk
输出
str1:qwertyui str2:asdfghjk str1:asdfghjk str2:qwertyui Program ended with exit code: 0
#include <iostream> #include <string> using namespace std; int main() { string str1,str2; cin>>str1>>str2; cout<<str1.compare(str2)<<endl; return 0; }
输入
diwguc aschsdnv
输出
3 Program ended with exit code: 0
size()函数和length()函数
#include <iostream> #include <string> using namespace std; int main() { string str1; cin>>str1; cout<<str1.size()<<endl; cout<<str1.length()<<endl; return 0; }
输入
dchiascnsc
输出
10 10 Program ended with exit code: 0
复制字符串s2到s1
链接s2到s1的末尾
返回字符串s1的长度
若s1和s2是相同的,则返回0,s1< s2,返回值小于0,若s1>s2,返回值大于0
返回一个指针,指向字符串s1中字符ch第一次出现的位置
返回一个指针,指向字符串s1中字符串s2的第一次出现位置
从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中strcpy与memcpy的区别:一、复制的内容不一样。strcpy只能复制字符串,而memcpy能够复制任意内容,例如字符数组、整型、结构体、类等。二、复制的方法不一样。strcpy不须要指定长度,它遇到被复制字符的串结束符"\0"才结束,因此容易溢出。memcpy则是根据其第3个参数决定复制的长度。三、用途不一样。一般在复制字符串时用strcpy,而须要复制其余类型数据时则通常用memcpy