当前位置:网站首页>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<
边栏推荐
猜你喜欢
随机推荐
Nuget找不到包的问题处理
Kotlin算法入门求完全数
对比学习系列(三)-----SimCLR
【415. 字符串相加】
【C语言】每日一题,求水仙花数,求变种水仙花数
mysql添加用户以及设置权限
进阶-指针
数据库无法启动,报无法分配内存,怎么处理
C Primer Plus(6) 中文版 第1章 初识C语言 1.1 C语言的起源 1.2 选择C语言的理由 1.3 C语言的应用范围
Openlayers 聚合图、权重聚合图以及聚合图点击事件
Kotlin算法入门计算水仙花数
如何仅更改 QGroupBox 标题的字体?
磁盘管理:磁盘结构
第一次因没有找到iframe元素而怀疑selenium4是不是有bug?
Unity3D - modification of the Inspector panel of the custom class
Essential C# scripting skills for Unity developers
gRPC系列(二) 如何用Protobuf组织内容
Go 语言的诞生
基于 VIVADO 的 AM 调制解调(3)仿真验证
Notable NFT development trends in 2022