string实用函数

版权声明:SupremeBeast3_ https://blog.csdn.net/weixin_43359312/article/details/89137732

string实用函数

参考网站:https://blog.csdn.net/qq_37941471/article/details/82107077
------构造------

#include <iostream>
#include <string>
using namespace std;
int main() {
	string str1;	//生成一个空字符串
	string str2(str1);	//生成str1的复制串
	string str3(str2, 1, 3); //将字符串str2中从下标1开始、长度为3的部分作为字符串初值
	string str4(3, 'c');	//生成有3个'c'字符的字符串
	string str5(str4, 1); //将字符串str4中从下标1开始到字符串结束的位置作为字符串初值
	return 0;
}

------比较------

#include <iostream>
#include <string>
using namespace std;
int main() {
	string str1 = "123456", str2 = "123567";
	str1.compare(str2);	//前面减去后面的ASCII码,>0返回1 <0返回-1 相同返回0
	str1.compare(0, 3, str2);	//str1从0开始的3个字符与str2比较
	str1.compare(0, 3, str2, 0, 3);	//str1与str2从0开始的3个字符相比较
	return 0;
}

------插入------

#include <iostream>
#include <string>
using namespace std;
int main() {
	string str1;
	str1.push_back('a');	//在str1尾部插入字符'a'
	str1.insert(str1.begin(), 'b');	//从开始插入字符'b'
	return 0;
}

------迭代------

#include <iostream>
#include <string>
using namespace std;
int main() {
	string str1;
	string::iterator iter;		//正向迭代器
	for (iter = str1.begin(); iter != str1.end(); iter++) cout << *iter;
	string::reverse_iterator riter;			//反向迭代器
	for (riter = str1.rbegin(); riter != str1.rend(); riter++) cout << *riter;
	return 0;
}

------擦除------

#include <iostream>
#include <string>
using namespace std;
int main() {
	string str1;
	str1.erase(1, 3);	//擦除位置1开始的3个字符
	return 0;
}

------替换------

#include <iostream>
#include <string>
using namespace std;
int main() {
	string str1;
	str1.replace(1, 3, "abc");	//将str1从1开始的3个字符替换为字符串"abc"
	str1.replace(1, 3, 3, 'a');	//将str1从1开始的3个字符替换为字符串"aaa"
	return 0;
}

------转换------

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
	string str1;		//另加algorithm 将字符转换成小写/大写 (::toupper)
	transform(str1.begin(), str1.end(), str1.begin(), ::tolower);
	return 0;
}

------查找------

#include <iostream>
#include <string>
using namespace std;
int main() {
	string str1;
	str1.find("abc");	 //找到返回首字母在字符串中的下标
	str1.find('i', 3);	//从位置3开始找到字符'i'返回其下标
	str1.rfind("abc");	//从尾部开始寻找字符串 返回的还是首字母在字符串中的下标
	str1.find_first_of("abc");	//找到第一个属于"abc"任一字符的位置返回其下标
	str1.find_last_of("abc");	//找到最后一个属于"abc"任一字符的位置返回其下标
	str1.find_first_not_of("abc");	//找到第一个不属于"abc"任一字符的位置返回其下标
	str1.find_last_not_of("abc");	//找到最后一个不属于"abc"任一字符的位置返回其下标
	return 0;
}

------排序------

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
	string str1;
	sort(str1.begin(), str1.end());	//另加algorithm
	return 0;
}

------分割------

#include <iostream>
#include <string>
using namespace std;
int main() {
	char str[] = "I,am,a,student; hello world!";	//使用字符数组
	const char *Split = ",; !";		//Split 分割/分裂
	char *pos = strtok(str, Split);
	while (pos != NULL){ cout << pos << endl; pos = strtok(NULL, Split); }
	return 0;
}

------截取------

#include <iostream>
#include <string>
using namespace std;
int main() {
	string str1;
	string str2 = str1.substr(1, 3);	//截取str1从1开始3个字符
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43359312/article/details/89137732
今日推荐