网站建设需要租赁服务器吗,电商运营团队,嵌入式软硬件开发,响应网站C之string #include iostreamusing namespace std;/*string();//创建一个空的字符串string(const char* s);//使用字符串s初始化string(const string str);//使用一个string对象初始化另外一个string对象string(int n,char c);//使用n个字符c初始化*/void test1()…C之string #include iostreamusing namespace std;/*string();//创建一个空的字符串string(const char* s);//使用字符串s初始化string(const string str);//使用一个string对象初始化另外一个string对象string(int n,char c);//使用n个字符c初始化*/void test1()
{string s1;//默认构造const char *str hello woreld!;string s2(str);cout s2 s2endl;string s3(s2);cout s3 s3endl;string s4(5,b);cout s4 s4endl;
}int main()
{test1();cout Hello World! endl;return 0;
} #include iostream
#includestring
using namespace std;/*
string operator(const char* s);//char*类型字符串 赋值给当前的字符串
string operator(const string s);//把字符串s赋给当前的字符串
string operator(char c);//字符赋值给当前的字符串
string assign(const char *s);//把字符串s赋给当前的字符串
string assign(const char *s, int n);//把字符串s的前n个字符赋给当前的字符串
string assign(const string s);//把字符串s给当前字符串
string assign(int n, char c);//用n个字符c赋给当前字符串*/void test1()
{string s1 hello woreld!;cout s1 s1endl;string s2(s1);cout s2 s2endl;string s3 ;s3 b;cout s3 s3endl;string s4;s4.assign(hello woreld!);cout s4 s4endl;string s5;s5.assign(hello woreld!,5);cout s5 s5endl;string s6;s6.assign(s5);cout s6 s6endl;string s7;s7.assign(5,b);cout s7 s7endl;
}int main()
{test1();cout Hello World! endl;return 0;
} #include iostream
#includestring
using namespace std;/*
string operator(const char* str);//重载操作符
string operator(const char c);//重载操作符
string operator(const string str);//重载操作符
string append(const char *s);//把字符串s连接到当前字符串结尾
string append(const char *s, int n);//把字符串s的前n个字符连接到当前字符串结尾
string append(const string s);//同operator(const string str)
string append(const string sint posint n); //字符s中从pos开始的n个字符连接到字符串结尾*/void test1()
{string s1 I;cout s1 s1endl;s1 LOVE GAME;cout s1 s1endl;string s2 :;s1 s2;cout s1 s1endl;// s1.append( LOL DNF);// s1.append( LOL DNF,4);string s3 LOL DNF;//s1.append(s3);s1.append(s3,0,4);cout s1 s1endl;}int main()
{test1();cout Hello World! endl;return 0;
} #include iostream
#includestring
using namespace std;//查找
void test1()
{string str1 abcdefgde;int pos str1.find(de);if(pos -1){coutno findendl;}else{coutfinded , pos posendl;}//find和rfind的区别//find:从左往右查找rfind:从右往左查找int pos2 str1.rfind(de);if(pos2 -1){coutno findendl;}else{coutfinded , pos pos2endl;}
}int main()
{test1();cout Hello World! endl;return 0;
} void test2()
{string str1 abcdefgde;//将bc替换成2222str1.replace(1,3,2222);coutstr1endl;
}
int main()
{test2();cout Hello World! endl;return 0;
}#include iostream
#includestring
using namespace std;//查找
void test1()
{string str1 hello;string str2 xello;if(str1.compare(str2) 0){coutstr1 str2endl;}else if(str1.compare(str2) 0){coutstr1 str2endl;}else{coutstr1 str2endl;}
}int main()
{test1();cout Hello World! endl;return 0;
} #include iostream
#includestring
using namespace std;//string字符存取
void test1()
{string str1 hello;//coutstr1 str1endl;//通过[]访问单个字符for(unsigned int i 0;i str1.size();i){coutstr1[i] ;}coutendl;//通过at方式访问单个字符for(unsigned int i 0;istr1.size();i){coutstr1.at(i) ;}coutendl;//通过[]修改单个字符str1[0] x;coutstr1 str1endl;//通过at方式修改单个字符str1.at(1) x;coutstr1 str1endl;
}int main()
{test1();cout Hello World! endl;return 0;
} #include iostream
#includestring
using namespace std;//string字符插入和删除
void test1()
{string str1 hello;coutstr1 str1endl;//插入str1.insert(1,222);coutstr1 str1endl;//删除str1.erase(1,3);coutstr1 str1endl;
}int main()
{test1();cout Hello World! endl;return 0;
} #include iostream
#includestring
using namespace std;//string字符中求子串
void test1()
{string str1 hello;coutstr1 str1endl;string subStr str1.substr(2,2);coutsubStr subStrendl;
}//使用的例子 获取邮箱的用户名
void test2()
{string str2 zhangsansina.com;int pos str2.find();string subStr str2.substr(0,pos);coutsubStr subStrendl;
}int main()
{test2();cout Hello World! endl;return 0;
}