#include<iostream>
#include<string>
using std::string;
#define SoftName "job"
#define SoftVersion "2.3"
int main() {
int age;
std::cin >> age;
string x{"yvgewudi",0,4};//前包含后之前,注意中文
string str, ls;
ls = "用户的年龄是:";
//.length()和.size()等价
std::cout << x.length()<<std::endl;
std::cout << x.size()<<std::endl;
//std::to_string()//拼接int
str = x + ls + std::to_string(age);
std::cout << str<<std::endl;
//string解决未定义字符串拼接-->两者给后者定义(或者用()强制类型转换)
str = "我吃!==" + string{ "wc!" };
//多者->嵌套(原理是运算符从左到右)
str = "??" + ("!!" + string{ "66" });
str = "??" + string{ "!!" } +("66" + string{ "17" });
string strA = str;
//#define解决未定义字符串拼接
string strB;
strB = SoftName SoftVersion;//记得空格等同于strB="666" "888";
std::cout << strB <<" "<< strA << std::endl;
//已定义char类型可以相加
//.append:添加
string strC{ "123" };
strC.append("456789", 1, 3);//前含后不含
strC.append("777").append("888");
std::cout <<strC << " " << strC.length() <<std::endl;//中文不灵
//.substr:截取(起始位置,截取长度)
std::cout<< strC.substr(7) << " " <<strC.substr(6).substr(4,3) << " " << strC.length()<< std::endl;//3指的是截取长度
}
//strcpy_s安全地复制字符串
char dest[20];
const char src[] = "Hello NIKO!";
errno_t result=strcpy_s(dest, sizeof(dest), src);//errno_t用于错误报告的整型
std::cout << dest << std::endl;
云璃的String
于 2024-08-02 19:06:42 首次发布