C++ string assign()赋值常用方法
2017-02-09 10:29
453 查看
C++ string assign()赋值常用方法
函数assign()常用在给string类变量赋值.
常用方法有:
1,直接用另一个字符串赋值.
如str2.assign(str1);即用str1给str2赋值.
2,用另一个字符串的一个子串赋值
如str3.assign(str1, 2, 3);
3,用一个字符串的前一段子串赋值;
如str4.assign("World", 5);
4,用几个相同的字符,赋值.
如str5.assign(10, 'c');#include<iostream>
#include<string>
using namespace std;
//========================================
int main()
{
string str1("hello");
string str2;
string str3;
string str4;
string str5;
//====================================
str2.assign(str1);
str3.assign("World", 4);
str4.assign(str1, 2, 3);
str5.assign(10, 'c');
//====================================
cout<<str1<<endl;
cout<<str2<<endl;
cout<<str3<<endl;
cout<<str4<<endl;
cout<<str5<<endl;
//====================================
system("pause");
return 0;
}
//========================================
结果为:
hello
hello
Worl
llo
cccccccccc
函数assign()常用在给string类变量赋值.
常用方法有:
1,直接用另一个字符串赋值.
如str2.assign(str1);即用str1给str2赋值.
2,用另一个字符串的一个子串赋值
如str3.assign(str1, 2, 3);
3,用一个字符串的前一段子串赋值;
如str4.assign("World", 5);
4,用几个相同的字符,赋值.
如str5.assign(10, 'c');#include<iostream>
#include<string>
using namespace std;
//========================================
int main()
{
string str1("hello");
string str2;
string str3;
string str4;
string str5;
//====================================
str2.assign(str1);
str3.assign("World", 4);
str4.assign(str1, 2, 3);
str5.assign(10, 'c');
//====================================
cout<<str1<<endl;
cout<<str2<<endl;
cout<<str3<<endl;
cout<<str4<<endl;
cout<<str5<<endl;
//====================================
system("pause");
return 0;
}
//========================================
结果为:
hello
hello
Worl
llo
cccccccccc
相关文章推荐
- C++ string assign()赋值常用方法
- C++ string assign()赋值常用方法
- C++ string assign()赋值常用方法
- C++ string assign()赋值常用方法
- C++ string assign()赋值常用方法
- 对C++ string append方法的常用用法详解
- c++中string的常用方法
- C++ string append方法的常用用法
- C++ string append方法的常用用法
- c++中string的几种赋值方法
- C# Security 常用加密方法(针对string 的处理)
- JAVA对象转为Java String的几种常用方法
- NSString and NSMutableString常用方法
- [JAVA][String]StringUtils工具类的常用方法(isNotEmpty isNotBlank等)
- STD中string的常用方法总结
- JS_常用方法_String的扩展
- java对象转为java String的几种常用方法剖析
- ObjectARX C++中的常用方法
- 在Javascript中对String的一些方法扩展,实现常用的字符串处理。
- 课后作业(五)String对象的常用属性和方法