string::assign

从string类型变量或const char * 中读取部分字符串到string变量中:
赋值(assign)
语法:

basic_string &assign( const basic_string &str );
basic_string &assign( const char *str );
basic_string &assign( const char *str, size_type num );
basic_string &assign( const basic_string &str, size_type index, size_type len );
basic_string &assign( size_type num, char ch );
函数以下列方式赋值:

用str为字符串赋值,
用str的开始num个字符为字符串赋值,
用str的子串为字符串赋值,子串以index索引开始,长度为len
用num个字符ch为字符串赋值.

#include<iostream>
#include<string>
using namespace std;
int main(){
string s = "hello,world";
string m;
m.assign(s,5,3);
cout<<m<<endl;   //输出为 ,wo
const char*num=""qazwsx,ed;
string n;
n.assign(&num[0],3);
cout<<num<<endl; //输出为qazwsx,ed
cout<<n<<endl;  //输出位qaz
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值