One, string concept
string is the string type of STL, usually used to represent strings. Before using string, the string is usually represented by char*. Both string and char* can be used to represent strings, so what is the difference between the two.
Comparison of string and char*:
-
string is a class, char* is a pointer to a character.
string encapsulates char*, manages this string, is a char* type container.
-
String does not need to consider memory release and out of bounds.
string manages the memory allocated by char*. Every time string is copied, the value is maintained by the string class, so there is no need to worry about copying out of range and value out of range.
-
string provides a series of string manipulation functions
Find find, copy copy, delete erase, replace replace, insert insert
//string 转 char*
string str_1="string";
const char* cstr_1=str.c_str();
//char* 转 string
char* cstr_2="char";
string str_2(cstr);
Two, string constructor
-
Default constructor: string(); //Construct an empty string string s1.
-
Constructor: string(const string &str); //Construct a string the same as str. Such as string s1(s2).
-
Constructor with parameters:
string(const char *s); //initialize with string s
string(int n,char c); //initialize with n characters c
string s1; //调用无参构造
string s2(10, 'a');
string s3("abcdefg");
string s4(s3); //拷贝构造
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
/*
结果:
aaaaaaaaaa
abcdefg
abcdefg
*/
Three, string access character operation
Character operation of string class:
- const char &operator[] (int n) const; //Get characters through []
- const char &at(int n) const; //Get characters through the at method
- char &operator[] (int n);
- char &at(int n);
operator[] and at() both return the nth character in the current string, but there is a difference between the two.
The main difference is that at() throws an exception when it crosses the boundary, and [] returns (char) 0 when it just crosses the boundary. When it continues to cross the boundary, the compiler directly fails. If your program hopes to catch exceptions through try and catch, at() is recommended.
string s1 = "abcdefg";
//重载[]操作符
for (int i = 0; i < s1.size(); i++) {
cout << s1[i] << " ";
}
cout << endl;
//at成员函数
for (int i = 0; i < s1.size(); i++) {
cout << s1.at(i) << " ";
}
cout << endl;
//区别:[]方式 如果访问越界,直接挂了
//at方式 访问越界 抛异常out_of_range
try {
//cout << s1[100] << endl;
cout << s1.at(100) << endl;
}
catch (...) {
cout << "越界!" << endl;
}
/*
结果:
a b c d e f g
a b c d e f g
越界!
*/
Fourth, the operation of obtaining const char* from string
const char *c_str() const; //returns the first address of a string ending with'\0'
//string 转 char*
string str_1="string";
const char *cstr_1=str.c_str();
Five, the operation of copying string to the memory space pointed to by char*
int copy(char *s, int n, int pos=0) const;
Copy the n characters starting with pos in the current string to the character array starting with s, and return the actual number of copies. Pay attention to ensure that the space pointed to by s is large enough to accommodate the current string, otherwise it will cross the boundary.
string s1 = "abcdefg";
int n = 5;pose
int pose = 3;
char* s2 = (char*)malloc(n*sizeof(char));
if(s1.copy(s2, n, pose))
cout << s2 ;
else cout<<"error"<<endl;
/*
结果:
def
*/
Six, the length of the string
int length() const; //Returns the length of the current string. The length does not include the'\0' at the end of the string.
bool empty() const; //Whether the current string is empty
string s1 = "abcdefg";
string s2 = "";
int s1_length=s1.length();
bool s1_empty = s1.empty();
int s2_length = s2.length();
bool s2_empty = s2.empty();
cout << s1_length << " " << s1_empty << endl;
cout << s2_length << " " << s2_empty << endl;
/*
结果:
7 0
0 1
*/
Seven, string assignment
string &operator=(const string &s);//Assign string s to the current string
string &assign(const char *s); //Assign string s to the current string
string &assign(const char *s, int n); //Assign the first n characters of string s to the current string
string &assign(const string &s); //Assign string s to the current string
string &assign(int n,char c); //Use n characters c to assign the current string
string &assign(const string &s,int start, int n); //Assign n characters from start in string s to the current string
string s1;
string s2("appp");
s1 = "abcdef";
cout << s1 << endl;
s1 = s2;
cout << s1 << endl;
s1 = 'a';
cout << s1 << endl;
//成员方法assign
s1.assign("jkl");
cout << s1 << endl;
/*
结果:
abcdef
appp
a
jkl
*/
Eight, string string connection
string &operator+=(const string &s); //Connect the string s to the end of the current string
string &operator+=(const char *s);//Connect the string s to the end of the current string
string &append(const char *s); //Connect the string s to the end of the current string
string &append(const char *s,int n); //Connect the first n characters of string s to the end of the current string
string &append(const string &s); //同operator+=()
string &append(const string &s,int pos, int n);//Connect the n characters starting from pos in the string s to the end of the current string
string &append(int n, char c); //Add n characters to the end of the current string c
string s = "abcd";
string s2 = "1111";
s += "abcd";
s += s2;
cout << s << endl;
string s3 = "2222";
s2.append(s3);
cout << s2 << endl;
string s4 = s2 + s3;
cout << s4 << endl;
/*
结果:
abcdabcd1111
11112222
111122222222
*/
Nine, string comparison
int compare(const string &s) const; //Compare with string s
int compare(const char *s) const; //Compare with string s
The compare function returns 1 when >, -1 when <, and 0 when ==. The comparison is case-sensitive. Refer to the dictionary order when comparing. The uppercase A is smaller than the lowercase a.
string s1 = "abcd";
string s2 = "abce";
if (s1.compare(s2)==0) {
cout << "字符串相等!" << endl;
}
else {
cout << "字符串不相等!" << endl;
}
10. Substring of string
string substr(int pos=0, int n=npos) const; //returns a substring consisting of n characters starting with pos
string s = "abcdefg";
string mysubstr = s.substr(1, 3);
cout << mysubstr << endl;
/*
结果:
bcd
*/
11. Find and replace string
Find
int find(char c,int pos=0) const; //Find the position of character c in the current string starting from pos
int find(const char *s, int pos=0) const; //Find the position of the string s in the current string starting from pos
int find(const string &s, int pos=0) const; //Find the position of string s in the current string starting from pos
//If the find function can't find it, it returns -1
int rfind(char c, int pos=npos) const; //Find the position of the character c in the current string starting from pos from back to front
int rfind(const char *s, int pos=npos) const;
int rfind(const string &s, int pos=npos) const;
//rfind means reverse search, if it cannot be found, return -1
replace
string &replace(int pos, int n, const char *s);//Delete n characters starting from pos, and then insert the string s at pos
string &replace(int pos, int n, const string &s); //Delete n characters starting from pos, and then insert the string s at pos
void swap(string &s2); //Swap the current string and s2 value
// 字符串的查找和替换
string s1 = "wbm hello wbm 111 wbm 222 wbm 333";
size_t index = s1.find("wbm", 0);
cout << "index: " << index;
//求itcast出现的次数
size_t offindex = s1.find("wbm", 0);
while (offindex != string::npos){
cout << "在下标index: " << offindex << "找到wbm\n";
offindex = offindex + 1;
offindex = s1.find("wbm", offindex);
}
//替换
string s2 = "wbm hello wbm 111 wbm 222 wbm 333";
s2.replace(0, 3, "wbm");
cout << s2 << endl;
//求itcast出现的次数
offindex = s2.find("wbm", 0);
while (offindex != string::npos){
cout << "在下标index: " << offindex << "找到wbm\n";
s2.replace(offindex, 3, "WBM");
offindex = offindex + 1;
offindex = s1.find("wbm", offindex);
}
cout << "替换以后的s2:" << s2 << endl;
/*
结果:
index: 0在下标index: 0找到wbm
在下标index: 10找到wbm
在下标index: 18找到wbm
在下标index: 26找到wbm
wbm hello wbm 111 wbm 222 wbm 333
在下标index: 0找到wbm
在下标index: 10找到wbm
在下标index: 18找到wbm
在下标index: 26找到wbm
替换以后的s2:WBM hello WBM 111 WBM 222 WBM 333
*/
12. Interval deletion and insertion of String
string &insert(int pos, const char *s);
string &insert(int pos, const string &s);//Insert string s at position pos
string &insert(int pos, int n, char c); //Insert n characters at position pos c
string &erase(int pos=0, int n=npos); //Delete n characters from the beginning of pos and return the modified string
string s = "abcdefg";
s.insert(3, "111");
cout << s << endl;
s.erase(0, 2);
cout << s << endl;
/*
结果:
abc111defg
c111defg
*/