C++学习笔记-String对象

版权声明:本文为博主原创文章,转载需注明出处 https://blog.csdn.net/sunqin_csdn/article/details/85028480

String对象初始化

string初始化方法 解释
sting s; 创建空的字符串;
string s = “value”; 用值value来初始化字符串s;
string s(“value”); 用值value来初始化字符串s;
string s(s2); 用字符串s2来初始化字符串s;
string s(s2, pos); 从s2的位置pos处开始到最后的所有字符来初始化s;
string s(n, ‘c’); 用n个字符c来初始化s;
string s(b, e); 用迭代器b到e之间的内容初始化字符串s;
string s(chs, n); 用从字符数组chs的前n个字符来初始化s;
string s(s2, pos, len); 从s2的第pos个位置开始的len个字符初始化s;
#include<iostream>
#include<string>

int main()
{
	std::string s1;
	std::string s2 = "Come on, boy!";
	std::string s3("Hello World, Welcome to C++!");
	std::string s4(s3);
	std::string s5(5, 's'); // sssss
	std::string s6(s3.begin() + 6, s3.begin()+6+5); // World
	std::string s7(s3, 6); // World, Welcome to C++!
	
	char *chs = "Project Main."; // 字符指针表示字符串,该字符串的最后有一个'\0'
	char ch2[] = "C plus plus!"; // 字符数组表示字符串,最后有一个'\0';
	char ch3[] = { 'H', 'W' }; // 字符数组,后面没有'\0',因此不算是字符串;
	
	std::string s8(chs, 7); // Project // 从chs开始的前面7个字符
	std::string s9(ch2, 6); // C plus
	std::string s10(ch2 + 7, 5); // plus! // 从chs第7个字符开始的后面5个字符
	std::string s11(ch2); // C plus plus!

	//std::string s11(ch3); // 报错,ch3后面没有'\0';
	std::string s12(ch3, 2); // HW
	std::string s13(chs, 8, 5); // Main. // 从chs的第8个位置开始的5个字符, plus!
	std::string s14(chs, 8, 10); // Main. // 从chs的第8个位置开始的10个字符, 不够10个就取到chs的最后.

	std::cout << s1 << std::endl;
	std::cout << s2 << std::endl;
	std::cout << s3 << std::endl;
	std::cout << s4 << std::endl;
	std::cout << s5 << std::endl;
	std::cout << s6 << std::endl;
	std::cout << s7 << std::endl;
	std::cout << s8 << std::endl;
	std::cout << s9 << std::endl;
	std::cout << s10 << std::endl;
	std::cout << s11 << std::endl;
	std::cout << s12 << std::endl;
	std::cout << s13 << std::endl;
	std::cout << s14 << std::endl;

	//system("pause");
	return 0;
}

修改String对象:

1.通用迭代器方法修改string对象

String对象修改方法 解释
s.insert(p, t); 在迭代器 p 前面插入字符 t;
s.insert(p, n, t); 在迭代器 p 前面插入 n 个字符 t;
s.insert(p, b, e); 在迭代器 p 的位置插入迭代器 b 到 e 直接的所有元素;
s.assign(b, e); 将迭代器 b 和 e 之间的所有元素赋值给 s;
s.assign(n, t); 用 n 个字符 t 给 s 赋值;
s.erase(p ); 删除字符串 s 中迭代器 p 指向的元素;
s.erase(b, e); 删除字符串 s 中区间 b 到 e 之间的所有元素;

2.String独有方法修改String对象

String对象修改方法 解释
s.insert(pos, n, c); 在字符串s的下标为pos的位置插入n个字符c;
s.insert(pos, s2); 在字符串s的下标为pos的位置插入字符串s2;
s.insert(pos, s2, pos2, len); 在字符串s的下标为pos的位置插入从字符串s2的pos2位置开始长度为len的之间的字符;
s.insert(pos, chs, len); 在字符串s下标为pos的位置插入chs的前len个元素;(chs为C语言字符串类型)
s.insert(pos, s2, pos2); 在字符串s下标为pos的位置插入字符串s2的pos2开始到最后的所有字符;(s2为string类型)
s.insert(pos, chs); 在字符串s的下标为pos的位置插入字符串chs
s.assign(s2); 将字符串s2赋值给s;
s.assign(s2, pos, len); 将字符串s2的下标为pos长度为len之间的所有字符赋值给s
s.assign(chs, len); 将字符串chs的前len个字符赋值给s;
s.assign(chs); 将chs赋值给s
s.erase(pos, len); 删除字符串s中下标为pos,长度为len间的所有字符
#include<iostream>
#include<string>

int main()
{
	// 通用迭代方法,不一一例举
	std::string s1("abcdefg");
	std::string s2("aaabbbccc");
	s2.insert(++s2.begin(),'A');
	std::cout << s2 << std::endl; // aAaabbbccc
	s2.insert(s2.begin(), 3, 'A');
	std::cout << s2 << std::endl; // AAAaAaabbbccc
	s2.assign(++s1.begin(), --s1.end());
	std::cout << s2 << std::endl; // bcdef
	s2.erase(++s2.begin(), --s2.end()); 
	std::cout << s2 << std::endl; // bf

	// String类特有方法:
	s2.insert(1, 5, 'A'); //,01,在s2的下标为1的位置插入5个A;
	std::cout << s2 << std::endl; // bAAAAAf
	s2.insert(5, s1); // 02,在s2的下标为5的位置插入s2;
	std::cout << s2 << std::endl; // bAAAAabcdefgAf
	s2.insert(2, s1, 3); // 03,在s2的下标为2的位置插入s2从下标3开始的所有元素;
	std::cout << s2 << std::endl; // bAdefgAAAabcdefgAf
	s2.insert(7, s1, 0, 3); // 04,在s2的下标为7的位置插入s2从下标0开始的3个元素;
	std::cout << s2 << std::endl; // bAdefgAabcAAabcdefgAf

	char *ch = "Hello World!"; // 
	s2.assign(ch, 5); // 11,将字符串ch的前5个字符赋值给s2;
	std::cout << s2 << std::endl; // Hello
	s2.assign(ch); // 12,将字符串ch赋值s2;
	std::cout << s2 << std::endl; // Hello World!
	// ***: 注意,若下面ch为string类型,则含义不用,请与03比较***
	s2.insert(0, ch, 6); // 13,在s2的下标为0的位置插入字符串的前6个字符
	std::cout << s2 << std::endl; // Hello Hello World!
	s2.insert(0, ch); // 14,在s2的下标为0的位置插入整个ch
	std::cout << s2 << std::endl; // Hello World!Hello Hello World!

	s2.erase(5, 7); // 删除s2中从下标为5开始的7个字符;
	std::cout << s2 << std::endl; // HelloHello Hello World!
	s2.erase(s2.size() - 7, 7); // 删除s2中从下标为s2.size() - 7开始的7个字符;即删除s2的最后7个元素
	std::cout << s2 << std::endl; // HelloHello Hello
	s2.insert(s2.size(), 5, '!'); // 在s2的最后插入5个!
	std::cout << s2 << std::endl; // HelloHello Hello!!!!!
	s2.erase(2, 1).insert(2, "L"); // 将s2的第2个位置开始的1个元素删除,删除后的迭代器指向下一个元素,再在该元素指向的位置插入字符串,实现字符串替换
	std::cout << s2 << std::endl; // HeLloHello Hello
	s2[1] = 'E'; // 直接赋值修改s2中的元素;
	std::cout << s2 << std::endl; // HELloHello Hello

	//system("pause");
	return 0;
}
#include<iostream>
#include<string>

int main()
{
	char* ch1 = "Hello World";
	char ch2[] = "Welcome to C++";
	char ch3[] = {'A','B','C',};
	std::string s2 = "Hello C++";
	std::string s;

	// s.insert(pos, chs, len); // 在字符串s中插入chs的前len个元素;chs为C语言字符串类型,不是string类型;
	s = "abcdefg";
	s.insert(0, ch1, 5); // Helloabcdefg
	std::cout << s << std::endl;
	s = "abcdefg";
	s.insert(0, ch2, 7); // Welcomeabcdefg
	std::cout << s << std::endl;
	s = "abcdefg";
	s.insert(0, ch3, 3); // ABCabcdefg
	std::cout << s << std::endl;

	// s.assign(pos, s2, len); // 将字符串s的下标为pos长度为len之间的所有字符赋值给s;
	s = "abcdefg";
	s.insert(0, s2, 6); // C++abcdefg
	std::cout << s << std::endl;

	//system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/sunqin_csdn/article/details/85028480