C++ std::string类的使用

简介: C++ std::string类的使用

C++ string类

本文只展示使用方式

头文件

=========

#include <string>
Using std::string

初始化对象的方式

=====================

string s1 //默认初始化,s1是一个空串
string s2(s1) //s2是s1的副本
string s2 = s1 //等价于s2(s1),s2是s1的副本
string s3("value") //s3是字面值"value"的副本,除去字面值最后的空符号外
string s3 = "value" //等价于s3("value"),s3是字面值"value"的副本
string s4(n,'c')  //把s4初始化为由连续n个字符c组成的串

读写字符串

//将string对象读入s,遇到空白停止
string s;
cin >> s 
//每次读入一整行,直至到达文件末尾,并且输出非空的行.     
string line;    
while(getline(cin,line)
  if(!line.empty)
   cout << line <<endl;

常用成员函数列表

================

s.empty    //s为空返回true,否则返回false
  s.size     //返回s中字符的个数,返回string::size_type类型,用无符号整型接收返回值.
  s[n]       //返回s中第n个字符的引用,n从0开始计起
  s1+s2      //返回s1和s2连接后的结果
  getline(os,s)//从os输入流读取内容(遇到换行符停止)然后存入s这个string对象中.
  append() -- 在字符串的末尾添加字符
  find() -- 在字符串中查找字符串
  insert() -- 插入字符
  length() -- 返回字符串的长度
  replace() -- 替换字符串
  substr() -- 返回某个子字符串
//size_type定义为与unsigned型(unsigned int 或 unsigned long)具有相同含义。且可以保证足够大能够存储任意string对象的长度。

常用操作

  • string对象比较
    通过相等性运算符(==和!=)
    规则:先比较对象的长度,然后再注意比较对应位置的相异字符
  • string对象相加
  • :两字符串对象串接组成一个新的字符串对象
  • += : 将右侧字符串追加到左侧对象
  • 成员函数原型

字符串赋值

basic_string& assign (const E*s); //直接使用字符串赋值
basic_string& assign (const E*s, size_type n);
basic_string& assign (const basic_string & str, size_type pos, size_type n);
//将str的子串赋值给调用串
basic_string& assign (const basic_string& str);    //使用字符串的“引用”賦值
basic_string& assign (size_type n, E c) ; //使用 n个重复字符賦值
basic_string& assign (const_iterator first, const_iterator last);    //使用迭代器赋值

插入字符串

basic_string& insert (size_type p0 , const E * s); //插人 1 个字符至字符串 s 前面
basic_string& insert (size_type p0 , const E * s, size_type n); // 将 s 的前 3 个字符插入p0 位置
basic_string& insert (size_type p0, const basic_string& str);
basic_string& insert (size_type p0, const basic_string& str,size_type pos, size_type n); //选取 str 的子串
basic_string& insert (size_type p0, size_type n, E c); //在下标 p0 位置插入  n 个字符 c
iterator insert (iterator it, E c); //在 it 位置插入字符 c
void insert (iterator it, const_iterator first, const_iterator last); //在字符串前插入字符
void insert (iterator it, size_type n, E c) ; //在 it 位置重复插入 n 个字符 c
  • 删除字符串
iterator erase (iterator first, iterator last);
iterator erase (iterator it);
basic_string& erase (size_type p0 = 0, size_type n = npos);
//pos 表示要删除的子字符串的起始下标,len 表示要删除子字符串的长度。
//如果不指明 len 的话,那么直接删除从 pos 到字符串结束处的所有字符(此时 len = str.length - pos)。

提取子字符串

string substr (size_t pos = 0, size_t len = npos) const;
//pos 为要提取的子字符串的起始下标,len 为要提取的子字符串的长度。
  • 查找字符串
size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;
//第一个参数为待查找的子字符串,它可以是 string 字符串,也可以是C风格的字符串。第二个参数为开始查找的位置(下标);
//如果不指明,则从第0个字符开始查找。

字符串末尾添加字符

/* 在字符串的末尾添加str */ 
 basic_string &append( const basic_string &str );
 basic_string &append( const char *str );
/* 在字符串的末尾添加str的子串,子串以index索引开始,长度为len */ 
  basic_string &append( const basic_string &str, size_type index, size_type len );
/* 在字符串的末尾添加str中的num个字符 */ 
  basic_string &append( const char *str, size_type num );
/* 在字符串的末尾添加num个字符ch */ 
  basic_string &append( size_type num, char ch );
/* 在字符串的末尾添加以迭代器start和end表示的字符序列 */ 
  basic_string &append( input_iterator start, input_iterator end );
/**
append函数常用的三个功能:
直接添加另一个完整的字符串:如str1.append(str2);
添加另一个字符串的某一段子串:如str1.append(str2, 11, 7);
添加几个相同的字符:如str1.append(5, ‘.’);注意,个数在前字符在后.上面的代码意思为在str1后面添加5个"."
**/

==========================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

字符串替换

/* 如果在一个字符串中标识出具体位置,便可以通过下标操作修改指定位置字符的值,或者替换某个子串。 */
basic_string& replace (size_type p0, size_type n0, const E * s);
//使用字符串 s 中的 n 个字符,从源串的位置 P0 处开始替换
basic_string& replace (size_type p0, size_type n0, const E *s, size_type n); 
//使用字符串 s 中的 n 个字符,从源串的位置 P0 处开始替换 1 个字符
basic_string& replace (size_type p0, size_type n0, const basic_string& str);
//使用字符串 s 中的 n 个字符,从源串的位置 P0 处开始替换
basic_string& replace (size_type p0, size_type n0, const basic_string& str, size_type pos, size_type n); 
//使用串 str 的子串 str [pos, pos + n-1] 替换源串中的内容,从位置 p0 处开始替换,替换字符 n0 个
basic_string& replace (size_type p0, size_type n0, size_type n, E c); 
//使用 n 个字符 'c' 替换源串中位置 p0 处开始的 n0 个字符
basic_string& replace (iterator first0, iterator last0, const E * s);
//使用迭代器替换,和 1) 用法类似
basic_string& replace (iterator first0, iterator last0, const E * s, size_type n);
//和 2) 类似
basic_string& replace (iterator first0, iterator last0, const basic_string& str); 
//和 3) 类似
basic_string& replace (iterator first0, iterator last0, size_type n, E c);
//和 5) 类似
basic_string& replace (iterator first0, iterator last0, const_iterator first, const_iterator last);
//使用迭代器替换

==========================================================================================================================================================================================================================================================================================================================

综合示例

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str1 ("123456");
    string str;
    str.assign (str1); //直接赋值
    cout << str << endl;
    str.assign (str1, 3, 3); //赋值给子串
    cout << str << endl;
    str.assign (str1,2,str1.npos);//赋值给从位置 2 至末尾的子串
    cout << str << endl;
    str.assign (5,'X'); //重复 5 个'X'字符
    cout << str << endl;
    string::iterator itB;
    string::iterator itE;
    itB = str1.begin ();
    itE = str1.end();
    str.assign (itB, (--itE)); //从第 1 个至倒数第 2 个元素,赋值给字符串 str
    cout << str << endl;
    string A("ello");
    string B ;
    B.insert(1,A);
    cout << B << endl;
    A = "ello";
    B = "H";
    B.insert (1,"yanchy ",3);
    cout<< B <<endl;
    A = "ello";
    B = "H";
    B.insert (1,A,2,2);
    cout << B << endl;
    A="ello";
    B.insert (1 , 5 , 'C');
    cout << B << endl;
    A = "ello";
    B.clear();
    string::iterator it = B.begin() + 1;
  const char c = *(A.begin()+2);    
  B.insert(it, c);
  cout << "插入:" << B << endl;
  return 0;
}
}
#include <iostream>
#include <string>
using namespace std;
int main ()
{
    string str1 ("123456");
    string str2 ("abcdefghijklmn");
    string str;
    str.assign(str1);
    cout << str << endl;
    str.assign (str1 , 3, 3);
    cout << str << endl;
    str.assign (str1, 2, str1.npos);
    cout << str << endl;
    str.assign (5, 'X');
    cout << str << endl;
    string::iterator itB;
    string::iterator itE;
    itB = str1.begin ();
    itE = str1.end();
    str.assign (itB, (--itE));
    cout << str << endl;
    str = str1;
    cout << str << endl;
    str.erase(3);
    cout << str << endl;
    str.erase (str.begin (), str.end());
    cout << ":" << str << ":" << endl;
    str.swap(str2);
    cout << str << endl;
    string A ("ello");
    string B ("H");
    B.insert (1, A);
    cout << B << endl;
    A = "ello";
    B ='H';
    B.insert (1, "yanchy ", 3);
    cout << "插入: " << B << endl;
    A = "ello";
    B = "H";
    B.insert(1,A,2,2);
    cout << "插入:" << B << endl;
    A = "ello";
    B = "H";
    B.insert (1,5,'C');
    cout << "插入:" << B << endl;
    A = "ello";
    B = "H";
    string::iterator it = B.begin () +1;
    const string::iterator itF = A.begin ();
    const string::iterator itG = A.end ();
    B.insert(it,itF,itG);
    cout<<"插入:"<< B << endl;
    A = "ello";
    B = "H";
    cout << "A = " << A <<", B = " << B << endl ;
    B.append (A);
    cout << "追加:" << B << endl;
    B = "H";
    cout << "A = "<< A << ", B= " << B << endl;
    B.append("12345", 2);
    cout << "追加:" << B << endl;
    A = "ello";
    B = "H";
    cout << "A = " << A << ", B= " << B << endl;
    B.append ("12345", 2, 3);
    cout << "追加:" << B << endl;
    A = "ello";
    B = "H";
    cout << "A = " << A <<", B = " << B << endl;
    B.append (10 , 'a');
    cout << "追加:"<< B << endl;
    A = "ello";
    B = "H";
    cout << "A = " << A << ", B = " << B << endl;
    B.append(A.begin() , A.end());
    cout << "追加:" << B << endl;
    cin.get();
    return 0;
}


目录
相关文章
|
5天前
|
存储 编译器 C语言
c++的学习之路:5、类和对象(1)
c++的学习之路:5、类和对象(1)
19 0
|
5天前
|
C++
c++的学习之路:7、类和对象(3)
c++的学习之路:7、类和对象(3)
19 0
|
3天前
|
设计模式 Java C++
【C++高阶(八)】单例模式&特殊类的设计
【C++高阶(八)】单例模式&特殊类的设计
|
3天前
|
编译器 C++
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象
|
8天前
|
存储 安全 C语言
【C++】string类
【C++】string类
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
29天前
|
存储 C++ 容器
C++入门指南:string类文档详细解析(非常经典,建议收藏)
C++入门指南:string类文档详细解析(非常经典,建议收藏)
38 0
|
29天前
|
存储 编译器 C语言
C++入门: 类和对象笔记总结(上)
C++入门: 类和对象笔记总结(上)
34 0
|
9天前
|
编译器 C++
标准库中的string类(上)——“C++”
标准库中的string类(上)——“C++”
|
10天前
|
编译器 C++
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”