C++教程网 第26章

  • string介绍
  • string对象的定义和初始化
  • 常用成员函数
string类型支持长度可变的字符串,C++标准库将负责管理与存储字符相关的内存,以及提供各种有用的操作
typedef basic_string<char> string;
typedef basic_string<wchar_t> wstring; 
要使用string类型对象,必须包含相关头文件
#include <string>
using std::string;

对象的定义和初始化

string s1;                   // 默认构造函数,s1为空串
string s2(s1);            // 将s2初始化为s1的一个副本
string s3("value");    // 将s3初始化为一个字符串字面值副本
string s4(n, 'c');       // 将s4初始化为字符‘c’的n个副本

常用的成员函数

 

成员函数

功能描述

a)          

size()

得到字符串的大小

b)          

length()

同上

c)          

empty()

判断是否为空

d)          

substr()

截取字符串

e)          

find()

在字符串中查找字符或者字符串

f)           

rfind()

在字符串中反向查找

g)          

replace()

替代

h)          

compare()

比较字符串

i)            

insert()

插入字符

j)            

append() 

追加字符

k)          

swap()     

交换字符串

l)            

重载运算符

[ ],+=,=,+,>,<,>=,<=,!=,==,>>,<<等


#include <string>
#include <iostream>
using namespace std;
int main()
{
	string strinfo=" //*---Hello World!......------";
	string strset= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	string::size_type first = strinfo.find_first_of(strset);
	if(first == string::npos) 
		cout<<"not find any characters"<<endl; 
	string::size_type last = strinfo.find_last_of(strset);
	if(last == string::npos)
		cout<<"not find any characters"<<endl;
	cout << strinfo.substr(first, last - first + 1)<<endl;
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值