string类的常用方法整理

参考博客
参考视频

简介:

string是C++、java、VB等编程语言中的字符串,字符串是一个特殊的对象,属于引用类型。 在java、C#中,String类对象创建后,字符串一旦初始化就不能更改,因为string类中所有字符串都是常量,数据是无法更改,由于string对象的不可变,所以可以共享。对String类的任何改变,都是返回一个新的String类对象。 C++标准库中string类以类型的形式对字符串进行封装,且包含了字符序列的处理操作。

string的初始化

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str;
	//建立一个空字符串
	string str1("Hello");
	cout << str1 << endl;
	string str2 = "World";
	cout << str2 << endl;
	string str3(8,'x');
	//这个是先填写字符个数再写填入字符
	cout << str3 << endl;
	//string str4 = 'x';直接把字符赋给字符串是错误的
	//string str4('x');
	//string str5=22;赋数字也是错误的
	//string str5(22);
	//但是可以先定义一个空的字符串,然后再把字符赋值给string对象
	//相当于
	string str4;
	str4 = 'x';
	cout << str4 << endl;
	// 相当与先设置一个空串,然后空串里面只有一个字符x
	//string str5;
	//str5 = 6;
	//cout << str5 << endl;
	//这个语法没问题,但操作起来出错,输出不是6
	string str5(str1, 1, 2);
	cout << str5 << endl;
	return 0;
}

输出结果:

Hello
World
xxxxxxxx
x
el

与c字符数组转换


string str2("Hello World!"), str3;
char   str4[50];
cout << "输入 C 字符串" << endl;
scanf("%s",str4);
str3= str4;

string类的读取

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str1;
	cin >> str1;
	cout << str1 << endl;
	//cout读取和scanf("%s");一样,读取到空格截至
	string str2;
	getline(cin,str2);
	cout << str2 << endl;
	//读取到换行截至,和gets一样
	return 0;
}

输入:Hello Hello World
输出:
Hello
Hello World//前面有个空格
这里顺便区分一下getline和cin.getline

使用 C++ 字符数组与使用 string 对象还有另一种不同的方式,就是在处理它们时必须使用不同的函数集。例如,要读取一行输入,必须使用 cin.getline 而不是 getline 函数。这两个的名字看起来很像,但它们是两个不同的函数,不可互换。

与 getline 一样,cin.getline 允许读取包含空格的字符串。它将继续读取,直到它读取至最大指定的字符数或直到按下了回车键。以下是其用法示例:
cin.getline(sentence, 20);//sentence是数组名

cin.getline 函数使用两个用逗号分隔的参数。第一个参数是要存储字符串的数组的名称。第二个参数是数组的大小。当 cin.getline 语句执行时,cin 读取的字符数将比该数字少一个,为 null 终止符留出空间。这样就不需要使用 setw 操作符或 width 函数。以上语句最多可读取 19 个字符,null 终止符将自动放在数组最后一个字符的后面。

#include <iostream>
using namespace std;
int main()
{
  const int SIZE = 81;
  char sentence[SIZE];
  cout << "Enter a sentence: ";
  cin.getline (sentence, SIZE);
  //最多可以读取 80 个字符:
  cout << "You entered " << sentence << endl;
  return 0;
}

计算字符串的长度和内存大小

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str = "Hello World";
	cout << str.size() << endl;
	cout << str.length() << endl;
	cout<<str.capacity()<<endl;//重新分配内存之前,string对象能包含的最大字符数
	cout << sizeof(str) << endl;
	return 0;
}

11
11
15
28
//str只是一个指针,所以内存大小与字符串长度无关

string的复制

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str = "Hello World";
	string str1 = "xxxxxxxxxx";
	string str2;
	str1.assign(str);
	cout << str1 << endl;
	//讲str复制给str1
	//string str1(str)效果一样
	str1.assign(str, 4, 5);//字符串,开始坐标,字符个数
	//string str1(str,4,5)
	cout << str1 << endl;
	//将str从坐标4开始数五个字符复制给str1
	
	str2 = str;
	//string str2(str);效果一样
	cout << str2 << endl;
	return 0;
}

Hello World
o Wor
Hello World

字符串的连接

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str1 = "Hello World";
	string str2 = "xxxxxxxxxx";
	cout << str1 + str2 << endl;

    str1 = "Hello World";
	str2 = "xxxxxxxxxx";
	str1.append(str2);//把str2连接到str1,str2不变
	cout << str1 << endl;
	cout << str2 << endl;

	str1 = "Hello World";
	str2 = "xxxxxxxxxx";
	str1.append(str2,3,5);//连接部分字符串来源,开始坐标,连接个数
	cout << str1 << endl;
	cout << str2 << endl;

	return 0;
}

Hello Worldxxxxxxxxxx
Hello Worldxxxxxxxxxx
xxxxxxxxxx
Hello Worldxxxxx
xxxxxxxxxx

取字符串类的子字符串

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str1 = "Hello World";
	string str;
	str = str1.substr(3, 4);//初始坐标,子字符串个数(后一个没写默认到结尾)
	cout << str << endl;
	str1 = str1.substr(3);//初始坐标,子字符串个数(后一个没写默认到结尾)
	cout << str1 << endl;
	return 0;
}

lo W
lo World

string的比较

==
.>=
<
<=
!=

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str1 = "aaa";
	string str2 = "aaa";
	cout << (str1 == str2) << endl;
	string str3 = "aaa";
	string str4 = "aab";
	cout << (str3 >= str4) << endl;
	return 0;
}

1
0

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str1 = "aaa",str2 = "aaa";
	cout << str1.compare(str2) << endl;

	str1 = "aaa", str2 = "aab";
	cout << str1.compare(str2) << endl;
	//str1 < str2 --> -1

	str1 = "aac", str2 = "aab";
	cout << str1.compare(str2) << endl;
	cout << str2.compare(str1) << endl;
	//str1 > str2 --> 1
	//str2 < str1 --> -1

	//compare比较更高级因为它可以部分比较
	cout << str1.compare(0, 1, str2, 0, 1) << endl;
	//str1 0-1和str2 0-1比较
	cout << str1.compare(1, 2, str2, 0, 1) << endl;
	return 0;
}

0
-1
1
-1
0
1

string的交换

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
	string str1 = "Hello", str2 = "World";
	cout << str1 << " " << str2 << endl;
	str1.swap(str2);
	cout << str1 << " " << str2 << endl;
	return 0;
}

Hello World
World Hello

find查找字串(正序,倒叙,指定位置查找)

1.find方法会从前往后找第一次出现目标串的位置,如果找到,则返回起始下标。
2.rfind方法会从后往前找第一次出现目标串的位置,如果找到,则返回起始下标。
3.从下标17的地方开始找字串"chi"
如果找不到字串,则返回string::npos
//未找到,返回-1

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str = "wo jin tian yao chi huo guo chi huo guo";
	cout << (str.find("chi huo guo")) << endl;//16
	cout << (str.rfind("chi huo guo")) << endl;//28
	cout << (str.find("chi", 17)) << endl;
	cout << (str.find("aa", 17)) << endl;
	return 0;
}

16
28
28
4294967295

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str = "Hello World";
	cout << str.find_first_of("ello") << endl;
	cout << str.find_first_of("aaa") << endl;
	//find_first_of()正序查找()里面字符串的任意一个字符第一次出现的地方
	//如果找不到,返回string::npos
	cout << str.find_last_of("ello") << endl;
	cout << str.find_last_of("aaa") << endl;
	//find_first_of()倒序查找()里面字符串的任意一个字符第一次出现的地方
	//如果找不到,返回string::npos
	cout << str.find_first_not_of("Hello") << endl;
	cout << str.find_first_not_of("aaa") << endl;
	//find_first_not_of()正序查找第一次不等于()里字符串的位置
	//如果找不到,返回string::npos
	cout << str.find_last_not_of("World") << endl;
	cout << str.find_last_not_of("aaa") << endl;
	//find_first_not_of()倒序查找第一次不等于()里字符串的位置
	//如果找不到,返回string::npos
	return 0;
}

1
4294967295
9
4294967295
5
0
5
10

删除字符串

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str = "Hello World";
	str.erase();
	cout << str << endl;
	cout << str.size() << endl;
	str = "Hello World";
	str.erase(4);
	//删除开始的指定下标,没填就默认从0开始删除
	cout << str << endl;
	cout << str.size() << endl;
	string str1 = "Hello";
	str1.erase(2, 1);//删除起始位置,删除字符个数 
	cout << str1;
	return 0;
}

(这里有一行空行)
0
Hell
4
Helo

清空字符串

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str = "Hello World";
	cout << str << endl;
	str = "";
	//==str.clear();
	cout << str << endl;
	return 0;
}

Hello World
空行

字符串的替换

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str = "Hello World";
	str.replace(2,2,"haha");
	//替换开始的下标,连续替换的字符个数
	//就是ll被替换成haha
	cout << str << endl;
	str = "Hello World";
	str.replace(2, 2, "haha",1,3);
	cout << str << endl;
	return 0;
}

Hehahao World
Heahao World

字符串的插入

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str1 = "Hello World";
	string str2 = "happy";
	cout<<str1.insert(5, str2)<<endl;
	//被插入字符串,开始插入坐标,插入字符串
	str1 = "Hello World";
	str2 = "happy";
	cout << str1.insert(5,str2,2,3) << endl;
	return 0;
}

Hellohappy World
Helloppy World

得出字符串的指针

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str = "Hello World";
	printf("%s\n", str.c_str() + 1);
	cout << str.c_str() + 2 << endl;
	//cout<<str<<endl;
	//"Hello World"
	return 0;
}
  //  字符串的指针==字符串的名字.c_str();
  //  string str15= "Hello World";
  //  printf("%s\n", str15.c_str() );

ello World
llo World

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str1 = "123.4hello";
	cout<<atof(str1.c_str())<<endl;
	string str2 = "1234Hello";
	cout << atof(str2.c_str()) << endl;
	return 0;
}

123.4
1234

字符串的逆序

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
	string str = "Hello World";
	reverse(str.begin(), str.end());
	//填的是逆序的范围
	cout << str << endl;
	str = "Hello World";
	reverse(str.begin(),str.begin()+2);
	//0-2逆序
	cout << str << endl;
	return 0;
}

dlroW olleH
eHllo World

字符串的排序

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
	string str = "Hello World";
	sort(str.begin(),str.end());
	cout << str << endl;
	str = "Hello World";
	sort(str.begin()+5, str.end());
	cout << str << endl;
	return 0;
}

HWdellloor
Hello Wdlor

posted on 2021-02-08 16:49  不依法度  阅读(111)  评论(0)    收藏  举报

导航