string类

目录

概念

string类的常见接口使用

构造字符串对象

 string类对象常用容器操作

 string类对象的遍历操作

string类对象的修改操作


概念

string类是STL六大组件容器中的基础容器。相对于C语言的字符串,结束标志是'\0',而C++的字符串并不会那'\0'作为结束标志。

string类的官方文档介绍

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s("hello");
	char arr[] = { "hello" };
	cout << s << endl;
	cout << arr << endl;

	//将第二个l设置为'\0'
	s[3] = '\0';
	arr[3] = '\0';
	cout << s << endl;
	cout << arr << endl;

	system("pause");
	return 0;
}

 

string类的常见接口使用

需要包含头文件#include<string>

构造字符串对象

constructor

(1)构造一个空字符串,例如:string s();

(2)利用一个现有字符串拷贝构造一个新串,例如:string s1("hello");  string s2(s1);

(3)利用一个现有的串,从该字符串的pos位置开始拷贝构造len个字符,如果不写后面两个参数,默认直接拷贝整个串;如果最后一个参数不写,默认从pos位置开始拷贝到结尾。参数不能间隔着给,必须连续

(4)用C-string来构造string类对象。

 (5)从C语言类型的字符串中复制前n个字符构造新串。

 (6)用n个字符c构造字符串。

 (7)利用一个现有串的迭代器构造新串。暂时没学可以先了解一下。

 string类对象常用容器操作

size :返回字符串有效字符个数

string s("hello");
cout << s.size() << endl;//5
s[3] = '\0';//字符'\0'在这里也算有效字符
cout << s.size() << endl;//5

length :返回字符串有效字符长度

string s("hello");
cout << s.length() << endl;//5
s[3] = '\0';
cout << s.length() << endl;//5

capacity:返回字符串总空间大小

string s("hello");
//这里涉及到底层实现是如何增容的
//感兴趣的话可以看下STL源码剖析
cout << s.capacity() << endl;//15

empty :检测字符串是否是空串,如果是,就返回true,否则返回false

string s("hello");
if (s.empty())
	cout << "空串" << endl;
else
	cout << "不是空串" << endl;

clear :清空串里的有效字符

string s("hello");
s.clear();//此时s就是空串

reserve :给字符串预留空间(不做初始化操作),避免后期频繁增容效率低;如果数值小于当前容量,则不会做改动

string s("hello");
s.reserve(31);
cout << s.capacity() << endl;//31

resize :开空间并初始化。将有效字符个数设置成n个,不够的用字符c填充;不写的话默认用'\0'填充;

如果设置的个数小于当前有效字符个数,就会发生截断,将多出的有效字符去除。

如果等与,不做改动。

如果大于,后面会补设置的字符填充。

string s("hello");
s.resize(8,'A');
cout << s << endl;

s.resize(8,'Z');
cout << s << endl;

s.resize(3);
cout << s << endl;

 string类对象的遍历操作

operator[] :返回对字符串中pos位置的字符的引用。

string s("hello");
for (int i = 0; i < s.size(); i++)
{
	cout << s[i] << " ";
}
cout << endl;

begin  end  :begin获取字符串第一个字符的迭代器,end获取最后一个字符的下一个位置的迭代器。

string s("hello");
auto it = s.begin();
while (it != s.end())
{
	cout << *it << " ";
	++it;
}
cout << endl;

rbegin  rend :rbegin返回字符串最后一个字符的迭代器,rend返回字符串第一个字符的前一个位置的迭代器。

string s("hello");
//反向打印
auto it = s.rbegin();
while (it != s.rend())
{
	cout << *it << " ";
	++it;
}
cout << endl;

string类对象的修改操作

push_back :在字符串最后一个有效字符后插入字符

string s("hello");
char a = 'A';
s.push_back(a);
s.push_back('K');

append :在字符串后追加另一个字符串

string s1("hello");
string s2("world");
s1.append(s2);
s1.append("AAA");

operator+= :在字符串后追加另一个字符串或者字符

string s1("hello");
string s2("world");
char a = ' ';
s1 += a;
s1 += s2;
s1 += 'x';
s1 += "AAA";

c_str :返回C语言形式的字符串

string s1("hello");
string s2("world");
char a = ' ';
s1 += a;
s1 += s2;
s1 += '\0';
s1 += "AAA";
cout << s1 << endl;
cout << s1.c_str() << endl;//遇到'\0'结束

 find  npos :字符串从pos位置(不写默认从头往后找)开始往后找字符或字串,找到就返回该字符在字符串中的位置,否则返回结果就是npos,npos是无符号整型的最大值

string s("hello world");
if (s.find("lo", 0) != s.npos)
{
	cout << "找到了" << endl;
}
//从第三个字符开始往后找'w'
size_t ret = s.find('w', 3);
cout << ret << endl;//6

rfind :从pos位置开始往前找字符或者字串,找到返回出现位置,否则返回npos

string s("hello world");
if (s.rfind("lo", 8) != s.npos)
{
	cout << "找到了" << endl;
}
else
{
	cout << "找不到" << endl;
}
size_t ret = s.rfind('w', 8);
cout << ret << endl;//6

substr :从pos位置开始,截取n个字符然后返回截取到的字符串

string s1("hello world");
string s2 = s1.substr(0, 3);
cout << s2 << endl;//hel
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Java 中,String 是一个非常重要的,它用于处理字符串。以下是一些 String 的特点: 1. String 是不可变的 String 的实例是不可变的,一旦一个 String 对象被创建,它的值就无法被修改。例如,下面的代码创建了一个 String 对象: ``` String str = "Hello"; ``` 如果要将 str 的值修改为 "World",则需要重新创建一个新的 String 对象。这是因为 String 被设计为不可变的,这样可以提高安全性和线程安全性。 2. 字符串的比较 在 Java 中,可以使用 equals() 方法来比较两个字符串是否相等。例如: ``` String str1 = "Hello"; String str2 = "World"; if (str1.equals(str2)) { System.out.println("str1 和 str2 相等"); } else { System.out.println("str1 和 str2 不相等"); } ``` 还可以使用 equalsIgnoreCase() 方法来忽略字符串中的大小写。 3. 字符串的拼接 在 Java 中,可以使用加号(+)来拼接字符串。例如: ``` String str1 = "Hello"; String str2 = "World"; String str3 = str1 + " " + str2; System.out.println(str3); ``` 上面的代码会输出 "Hello World"。 4. 字符串的截取 可以使用 substring() 方法来截取字符串。例如: ``` String str = "Hello World"; String subStr = str.substring(6, 11); System.out.println(subStr); ``` 上面的代码会输出 "World",其中 substring() 方法的第一个参数是起始索引,第二个参数是结束索引(不包括在内)。 5. 字符串的查找 可以使用 indexOf() 和 lastIndexOf() 方法来查找字符串中某个字符或子字符串的位置。例如: ``` String str = "Hello World"; int index = str.indexOf("o"); int lastIndex = str.lastIndexOf("o"); System.out.println(index); // 输出 4 System.out.println(lastIndex); // 输出 7 ``` 其中 indexOf() 方法返回第一个匹配字符或子字符串的位置,lastIndexOf() 方法返回最后一个匹配字符或子字符串的位置。 除此之外,String 还有许多其他的方法,例如 trim() 方法可以去除字符串两端的空格,toUpperCase() 方法可以将字符串转换为大写,toLowerCase() 方法可以将字符串转换为小写,length() 方法可以获取字符串的长度等等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值