深入浅出之string

string 类是 STL 中 basic_string 模板实例化得到的模板类。其定义如下:

typedef basic_string <char> string;

1. 定义及初始化string对象

string 类有多个构造函数,用法示例如下:

1) string()字符串为空值 
如:string s1();  // si = ""
2)string(const char *str) 字符串为str中的字符
如:string s2("Hello");  // s2 = "Hello"
3)string(size_type length,char ch)
如:string s3(4, 'K');  // s3 = "KKKK"
4)string(const char *str,size_type index,size_type length) 字符串值为str以index开始的length个字符
如:string s4("12345", 1, 3);  //s4 = "234",即 "12345" 的从下标 1 开始,长度为 3 的子串
5)string(const char *str,size_type index)字符串值为str第index开始的所有字符
如:string S1(“Strings”,8)

注意: string 类没有接收一个整型参数或一个字符型参数的构造函数。下面的两种写法是错误的:

string s1('K');
string s2(123);

2. 字符串赋值

2.1 assign函数 

string s1("12345"), s2;
s3.assign(s1);  // s3 = s1
s2.assign(s1, 1, 2);  // s2 = "23",即 s1 的子串(1, 2)
s2.assign(4, 'K');  // s2 = "KKKK"
s2.assign("abcde", 2, 3);  // s2 = "cde",即 "abcde" 的子串(2, 3)

2.2 赋值运算符= 

string类已经对“=”进行了重载,可以用 char* 类型的变量、常量,以及 char 类型的变量、常量对 string 对象进行赋值。例如:

string s1;
s1 = "Hello";  // s1 = "Hello"
s2 = 'K';  // s2 = "K”

3. 字符串比较

 3.1 关系运算符

    ==,>,<,>=,<=,!=都已经重载,对字符串的操作可以适用。

 3.2 compare函数

 compare 成员函数有以下返回值:

  • 小于 0 表示当前的字符串小;
  • 等于 0 表示两个字符串相等;
  • 大于 0 表示另一个字符串小。
string s1("hello"), s2("hello, world");
int n = s1.compare(s2);
n = s1.compare(1, 2, s2, 0, 3);  //比较s1的子串 (1,2) 和s2的子串 (0,3)
n = s1.compare(0, 2, s2);  // 比较s1的子串 (0,2) 和 s2
n = s1.compare("Hello");
n = s1.compare(1, 2, "Hello");  //比较 s1 的子串(1,2)和"Hello”
n = s1.compare(1, 2, "Hello", 1, 2);  //比较 s1 的子串(1,2)和 "Hello" 的子串(1,2

4. 字符串的连接

 4.1 append函数

string s1("123"), s2("abc");
s1.append(s2);  // s1 = "123abc"
s1.append(s2, 1, 2);  // s1 = "123abcbc"
s1.append(3, 'K');  // s1 = "123abcbcKKK"
s1.append("ABCDE", 2, 3);  // s1 = "123abcbcKKKCDE",添加 "ABCDE" 的子串(2, 3)

4.2 =+符合赋值运算符 

string类已经将+运算符重载,可以对字符串进行连接

string s1(“hello”),s2(“world”);
s1 = s1+s2;

5. 字符串特性

5.1 获取字符串大小size或length

#include <QCoreApplication>
#include <string>
#include <iostream>

using namespace  std;


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

     string s,s1,s2;
     s1 = "hello";
     s2 = "world";
     s = s1+" "+s2;
     qDebug("%s-%d-%d",s.c_str(),s.length(),s.size());

    return a.exec();
}

5.2 获取字符容量

int capacity() const;

5.3 设置字符串长度resize

void resize(int len, char c); 

#include <QCoreApplication>
#include <string>
#include <iostream>

using namespace  std;


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

     string s,s1,s2;
     s1 = "hello";
     s2 = "world";
     s = s1+" "+s2;
     s.resize(12,'1');
     qDebug("%s-%d-%d",s.c_str(),s.length(),s.size());

    return a.exec();
}
输出结果:
hello world1-12-12

6. 字符串操作 

6.1 取得字符串中的字符 []

char& operator[](int n)

string s("hello world");
char c = s[1];

 6.2 取得字符串中的字符 at

char& at(int n)

string s("hello world");
char c = s.at(1);

注意:

【】和at区别在于,at函数提供范围检测,越界时会抛出异常;而【】不会提供下标检测。 

6.3  获得一个字符串 c_str

const char *c_str()const;

返回一个以null终止的字符串

字符数组转化成string类型
char ch [] = “ABCDEFG”;
string str(ch);//也可string str = ch;
或者
char ch [] = “ABCDEFG”;
string str;
str = ch;//在原有基础上添加可以用str += ch;

将string类型转换为字符数组
char buf[10];
string str(“ABCDEFG”);
length = str.copy(buf, 9);
buf[length] = ‘\0’;
或者
char buf[10];
string str(“ABCDEFG”);
strcpy(buf, str.c_str());//strncpy(buf, str.c_str(), 10);

6.4 求 string 对象的子串

string substr(int n = 0, int m = string::npos) const;

string s1 = "this is ok";
string s2 = s1.substr(2, 4);  // s2 = "is i"
s2 = s1.substr(2);  // s2 = "is is ok"

6.5 交换两个string对象的内容

swap 成员函数可以交换两个 string 对象的内容。例如:

string s1("West”), s2("East");
s1.swap(s2);  // s1 = "East",s2 = "West"

 6.6 查找子串和字符

string 类有一些查找子串和字符的成员函数,它们的返回值都是子串或字符在 string 对象字符串中的位置(即下标)。如果查不到,则返回 string::npos。string: :npos 是在 string 类中定义的一个静态常量。这些函数如下:

  • find:从前往后查找子串或字符出现的位置。
  • rfind:从后往前查找子串或字符出现的位置。
  • find_first_of:从前往后查找何处出现另一个字符串中包含的字符。例如:
  • s1.find_first_of("abc");  //查找s1中第一次出现"abc"中任一字符的位置
  • find_last_of:从后往前查找何处出现另一个字符串中包含的字符。
  • find_first_not_of:从前往后查找何处出现另一个字符串中没有包含的字符。
  • find_last_not_of:从后往前查找何处出现另一个字符串中没有包含的字符。
#include <QCoreApplication>
#include <string>
#include <iostream>

using namespace  std;
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    string s1("Source Code");
    int n;
    if ((n = s1.find('u')) != string::npos) //查找 u 出现的位置
        cout << "1) " << n << "," << s1.substr(n) << endl;
    //输出 l)2,urce Code
    if ((n = s1.find("Source", 3)) == string::npos)
        //从下标3开始查找"Source",找不到
        cout << "2) " << "Not Found" << endl;  //输出 2) Not Found
    if ((n = s1.find("Co")) != string::npos)
        //查找子串"Co"。能找到,返回"Co"的位置
        cout << "3) " << n << ", " << s1.substr(n) << endl;
    //输出 3) 7, Code
    if ((n = s1.find_first_of("ceo")) != string::npos)
        //查找第一次出现或 'c'、'e'或'o'的位置
        cout << "4) " << n << ", " << s1.substr(n) << endl;
    //输出 4) l, ource Code
    if ((n = s1.find_last_of('e')) != string::npos)
        //查找最后一个 'e' 的位置
        cout << "5) " << n << ", " << s1.substr(n) << endl;  //输出 5) 10, e
    if ((n = s1.find_first_not_of("eou", 1)) != string::npos)
        //从下标1开始查找第一次出现非 'e'、'o' 或 'u' 字符的位置
        cout << "6) " << n << ", " << s1.substr(n) << endl;
    //输出 6) 3, rce Code

    return a.exec();
}

6.7 替换子串

string s1("Real Steel");
s1.replace(1, 3, "123456", 2, 4);  //用 "123456" 的子串(2,4) 替换 s1 的子串(1,3)
cout << s1 << endl;  //输出 R3456 Steel
string s2("Harry Potter");
s2.replace(2, 3, 5, '0');  //用 5 个 '0' 替换子串(2,3)
cout << s2 << endl;  //输出 HaOOOOO Potter
int n = s2.find("OOOOO");  //查找子串 "00000" 的位置,n=2
s2.replace(n, 5, "XXX");  //将子串(n,5)替换为"XXX"
cout << s2 < < endl;  //输出 HaXXX Potter

6.8 删除子串 

string s1("Real Steel");
s1.erase(1, 3);  //删除子串(1, 3),此后 s1 = "R Steel"
s1.erase(5);  //删除下标5及其后面的所有字符,此后 s1 = "R Ste"

 6.9 插入子串

string s1("Limitless"), s2("00");
s1.insert(2, "123");  //在下标 2 处插入字符串"123",s1 = "Li123mitless"
s1.insert(3, s2);  //在下标 2 处插入 s2 , s1 = "Li10023mitless"
s1.insert(3, 5, 'X');  //在下标 3 处插入 5 个 'X',s1 = "Li1XXXXX0023mitless"

7. string与各类型转换

待补充

参考资料

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值