当前位置:网站首页>Detailed Explanation of the Level 5 Test Center of the Chinese Institute of Electronics (1)-string type string

Detailed Explanation of the Level 5 Test Center of the Chinese Institute of Electronics (1)-string type string

2022-08-11 08:42:00 mooczhimahu

string definition

string is a string type in C++ that is better, faster, and simpler than char in most ways.

Define a variable of type string:

#includeusing namespace std;int main(){string s;}

string is the type used to store strings, different from char.The char variable can only store characters, and the chan array can store strings, but strings are designed to store strings.Each variable of the string type variable can store a string. The variable space (length) is determined by the characters stored in it, and there is no need to define it yourself.In addition, there are arrays of type string, which can store multiple strings.

The input and output of strings are simple, no different from int variables.

#includeusing namespace std;int main(){string s;cin>>s;//or s={a,b,c,d,e,f};cout<

Usage of string

1. Concatenate the two strings with a + sign;

#includeusing namespace std;int main(){string s1,s2;cin>>s1>>s2;s1=s1+s2;cout<

2. Test length, use size;

3. Take character:

string type string.substr(start subscript, length);

Start from the starting subscript, take a few characters.

#includeusing namespace std;int main(){string s1,s2;cin>>s2;s1=s2.substr(2,3);cout<

4. Delete:

string type string.erase(start subscript,length);

From the beginning of the string, delete a few characters.

#includeusing namespace std;int main(){string s1,s2;cin>>s2;s1=s2.erase(2,3);cout<

5. Insert:

string type string 1.insert (starting subscript, string type string 2);

Insert string type string 2 before the starting subscript of string type string 1.

#includeusing namespace std;int main(){string s1,s2;cin>>s1>>s2;s1.insert(2,s2);cout<

原网站

版权声明
本文为[mooczhimahu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/223/202208110835393275.html