C Compared with the C language, C has greatly enhanced support for strings. In addition to using C-style strings, you can also use the built-in data type string. The string class is particularly convenient for processing strings because of the function encapsulation. , let’s count the functions of the string class below
First of all, if you want to call the string class, include its header file first#include <string>
string s1;//变量s1只是定义但没有初始化,所以其默认值为""即空字符串string s2="Hello World!";//变量s2在定义时同时被初始化/*string类的变量可以相互之间直接赋值,不需要像C语言一样,使用strcpy()函数一个字符一个字符的去赋值*///例如string s3=s2;//此时s3的内容和s2一样也是Hello World!//如果需要定义一个由很多相同字符组成的字符串时,还有另外的简便写法string s4(int n,char c);//s4是被初始化为由n的字符c组成的字符串
About the request In C language, we can use the strlen() function to find the length of a string. In C, we can also use strlen(s3); this method can find the actual length of the s3 string, but because C has a different relationship with the string class Languages are fundamentally different, so we generally call the string.length() function to find the length of a string
int len=0;len=string.length(s3); cout<<"s3字符串的长度为"<<len<<endl;
As we mentioned above, if a string-like string is assigned to another string-like string, Just assign the value directly, but what if the string class is assigned to the char* class or the char* class is assigned to the string class? Of course, it cannot be assigned directly, just look at the code
//string类赋值给string类string s1="hello world";string s2; s2=s1;//string类赋值给char*类string s1="hello world";char str[20]={0}; strcpy_s(str,s1.c_str());//char*类赋值给string类char str[20]="hello world";string s2; s2=str;
At the same time, string type variables can also use character array operations to change one of the variables in it, for example
#include <iostream>#include <string>string s1="this is my house";int i;//如果我们现在想改变里面某一个字符,可以直接将s1当成数组,找到对应的下标来改变i=6; s[i]='t';//这样就可以将第6个字符改成t了
With string class, we can use the " " or " = " operator to directly splice strings, which is very convenient. We no longer need to use strcat(), strcpy(), malloc() and other functions in C language to splice strings. You no longer have to worry about insufficient space and overflow. When using "" to splice strings, both sides of the operator can be string strings, a string string and a C-style string, or a string string and a char character.
Assignment of string class
string &operator=(const string &s);//把字符串s赋给当前字符串 string &assign(const char *s);//用c类型字符串s赋值string &assign(const char *s,int n);//用c字符串s开始的n个字符赋值string &assign(const string &s);//把字符串s赋给当前字符串string &assign(int n,char c);//用n个字符c赋值给当前字符串string &assign(const string &s,int start,int n);//把字符串s中从start开始的n个字符赋给当前字符串string &assign(const_iterator first,const_itertor last);//把first和last迭代器之间的部分赋给字符串
Connection of string
string &operator+=(const string &s);//把字符串s连接到当前字符串的结尾 string &append(const char *s);//把c类型字符串s连接到当前字符串结尾string &append(const char *s,int n);//把c类型字符串s的前n个字符连接到当前字符串结尾string &append(const string &s); //同operator+=()string &append(const string &s,int pos,int n);//把字符串s中从pos开始的n个字符连接到当前字符串的结尾string &append(int n,char c); //在当前字符串结尾添加n个字符cstring &append(const_iterator first,const_iterator last);//把迭代器first和last之间的部分连接到当前字符串的结尾
Substring of stringstring substr(int pos = 0,int n = npos) const;/ /Return a string consisting of n characters starting from pos
void swap(string &s2); //交换当前字符串与s2的值
Search for string
rfind() is very similar to find(), it also searches for sub-characters in a string string, the difference is that the find() function searches backwards from the second parameter, while the rfind() function searches up to the second parameter. If the sub-character has not been found by the subscript specified by the second parameter String, it returns an infinite value 4294967295
int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置int rfind(const char *s, int pos = npos) const;int rfind(const char *s, int pos, int n = npos) const;int rfind(const string &s,int pos = npos) const;//从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值
Insertion function of string class:
string &insert(int p0, const char *s);string &insert(int p0, const char *s, int n);string &insert(int p0,const string &s);string &insert(int p0,const string &s, int pos, int n);//前4个函数在p0位置插入字符串s中pos开始的前n个字符string &insert(int p0, int n, char c);//此函数在p0处插入n个字符citerator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置void insert(iterator it, const_iterator first, const_iterator last);//在it处插入[first,last)之间的字符void insert(iterator it, int n, char c);//在it处插入n个字符c
Regarding the usage of string class objects in C, today I talked about some basics.
Related recommendations:
Summary of usage of string class in standard C
The above is the detailed content of Summary of usage of string objects in C++. For more information, please follow other related articles on the PHP Chinese website!