笔记:string(C++标准库)

本文详细介绍了C++标准库中的std::string类的使用,包括赋值、附加、复制、删除、清空、替换、插入、查询、子串获取等操作,并通过实例代码展示了其用法。同时,还涵盖了与C风格字符串的交互以及字符串比较、长度计算等功能。

标准C++提供了两种字符串:

1.C语言风格的以'\0'字符结尾的字符数组

2.字符串类string。(Boost中又为string扩充了一些函数) 

总结一下std::string函数用法。

操作符:
string& operator=( const string& s );
string& operator=( const char* s );
string& operator=( char ch );
string operator+(const string& s1, const string& s2 );
string operator+(const char* s, const string& s2 );
string operator+( char c, const string& s2 );
string operator+( const string& s1, const char* s );
string operator+( const string& s1, char c );
bool operator==(const string& c1, const string& c2);

 
操作字符串:
附加:append(...)
赋值:assign(...)
复制:copy(...)
删除:erase(...)
清空:clear()
交换:void swap( const container& from );
替换:
string& replace( size_type index, size_type num, const string& str );
string& replace( size_type index1, size_type num1, const string& str, size_type index2, size_type num2 );
string& replace( size_type index, size_type num, const char* str );//用str替换从index开始的num个字符
string& replace( size_type index, size_type num1, const char* str, size_type num2 );
string& replace( size_type index, size_type num1, size_type num2, char ch );
string& replace( iterator start, iterator end, const string& str );
string& replace( iterator start, iterator end, const char* str );
string& replace( iterator start, iterator end, const char* str, size_type num );
string& replace( iterator start, iterator end, size_type num, char ch );
插入:
iterator insert( iterator i, const char& ch );
string& insert( size_type index, const string& str );
string& insert( size_type index, const char* str );//在index位置插入字符串str
string& insert( size_type index1, const string& str, size_type index2, size_type num );
string& insert( size_type index, const char* str, size_type num );
string& insert( size_type index, size_type num, char ch );
void insert( iterator i, size_type num, const char& ch );
void insert( iterator i, iterator start, iterator end );

 

查询字符串(搜索):
size_type find( const string& str, size_type index );
size_type find( const char* str, size_type index );//从index开始查找字符串str在当前串中的位置
size_type find( const char* str, size_type index, size_type length );
size_type find( char ch, size_type index );
size_type rfind( const string& str, size_type index );
size_type rfind( const char* str, size_type index );//从index开始从后向前查找字符串str在当前串中的位置
size_type rfind( const char* str, size_type index, size_type num );
size_type rfind( char ch, size_type index );
find_first_not_of(...) //从当前串中查找第一个不在串str中的字符出现的位置
find_first_of(...) //查找第一个出现的字符串
find_last_not_of(...) //find last absence of characters
find_last_of(...) //find last occurrence of characters

 
求子串
string substr( size_type index, size_type num = npos );//返回index开始的num个字符组成的字符串

 
转换为C语言字符串  const char* c_str();
比较字符串  compare(...)//负整数 this < str; 0 this == str; 正整数 this > str
判断字符串是否为空  bool empty() const;
求长度  size_type length() const;
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{
    //g++编译器
    string str1("Hello World!");
    string str2 = "welcome to China.";
    string str3 = "books";
    string str4 = "books";
    char buf[] = "12345678ab";
    string s1 = "I like go shopping.";
    string s2 = "effective C++";
    string s3 = "My cat's breath smells like cat food.";
    string s4 = "Tom is cat.";

    str1.append( str2, 3, 10 );//str1:"Hello World!come to Ch"
    str1.assign( str2, 4, 3 );//str1:"ome"
    str1.clear();//str1:""
    str3.compare(str4);//返回0
    str3.copy(buf,3);//buf:boo45678ab
    str1.empty();//返回true

    s1.erase( 1, 5 );//s1:"I go shopping."
    s1.erase( 3 );//s1:"I g"
    s1.erase();//s1:""

    int len = s2.length();//13
    string::size_type index;
    index = s2.find("abc",0);//失败返回string::npos 即-1
    index = s2.find("C++",0);//10

    int loc;
    loc = s3.rfind( "breath", 8 );//-1
    loc = s3.rfind( "breath", 20 );//9

    //cout << s2 << endl;
    //cout << s3 << endl;
    s2.swap(s3);
    //cout << s2 << endl;
    //cout << s3 << endl;

    s4.replace(4,2,"is not");//s4:"Tom is not cat."

    s4.insert(1,"abc");//s4:"Tabcom is not cat."

    string sTemp = s4.substr(7,6);//sTemp:"is not"


    return 0;
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值