C++ 字符串

C++ 提供了以下两种类型的字符串表示形式:

  • C 风格字符串
  • C++ 引入的 string 类类型

C 风格字符串

C 风格的字符串起源于 C 语言,并在 C++ 中继续得到支持。字符串实际上是使用 null 字符 \0 终止的一维字符数组。因此,一个以 null 结尾的字符串,包含了组成字符串的字符。

下面的声明和初始化创建了一个 RUNOOPS 字符串。由于在数组的末尾存储了空字符,所以字符数组的大小比单词 RUNOOPS 的字符数多一个。

char site[8] = {'R', 'U', 'N', 'O', 'O', 'P', 'S', '\0'};

依据数组初始化规则,您可以把上面的语句写成以下语句:

char site[] = "RUNOOPS";

以下是 C/C++ 中定义的字符串的内存表示:

其实,您不需要把 null 字符放在字符串常量的末尾。C++ 编译器会在初始化数组时,自动把 \0 放在字符串的末尾。让我们尝试输出上面的字符串:

实例

#include <iostream>
 
using namespace std;
 
int main ()
{
   char site[8] = {'R', 'U', 'N', 'O', 'O', 'P', 'S', '\0'};
 
   cout << "自学教程: ";
   cout << site << endl;
 
   return 0;
}

当上面的代码被编译和执行时,它会产生下列结果:

自学教程: RUNOOPS

C++ 中有大量的函数用来操作以 null 结尾的字符串:

序号函数 & 目的
1strcpy(s1, s2);
复制字符串 s2 到字符串 s1。
2strcat(s1, s2);
连接字符串 s2 到字符串 s1 的末尾。连接字符串也可以用 + 号,例如:
string str1 = "runoops"; string str2 = "google"; string str = str1 + str2;
3strlen(s1);
返回字符串 s1 的长度。
4strcmp(s1, s2);
如果 s1 和 s2 是相同的,则返回 0;如果 s1<s2 则返回值小于 0;如果 s1>s2 则返回值大于 0。
5strchr(s1, ch);
返回一个指针,指向字符串 s1 中字符 ch 的第一次出现的位置。
6strstr(s1, s2);
返回一个指针,指向字符串 s1 中字符串 s2 的第一次出现的位置。

下面的实例使用了上述的一些函数:

实例

#include <iostream>
#include <cstring>
 
using namespace std;
 
int main ()
{
   char str1[13] = "runoops";
   char str2[13] = "google";
   char str3[13];
   int  len ;
 
   // 复制 str1 到 str3
   strcpy( str3, str1);
   cout << "strcpy( str3, str1) : " << str3 << endl;
 
   // 连接 str1 和 str2
   strcat( str1, str2);
   cout << "strcat( str1, str2): " << str1 << endl;
 
   // 连接后,str1 的总长度
   len = strlen(str1);
   cout << "strlen(str1) : " << len << endl;
 
   return 0;
}

当上面的代码被编译和执行时,它会产生下列结果:

strcpy( str3, str1) : runoops
strcat( str1, str2): runoopsgoogle
strlen(str1) : 13

C++ 中的 String 类

C++ 标准库提供了 string 类类型,支持上述所有的操作,另外还增加了其他更多的功能。我们将学习 C++ 标准库中的这个类,现在让我们先来看看下面这个实例:

现在您可能还无法透彻地理解这个实例,因为到目前为止我们还没有讨论类和对象。所以现在您可以只是粗略地看下这个实例,等理解了面向对象的概念之后再回头来理解这个实例。

实例

#include <iostream>
#include <string>
 
using namespace std;
 
int main ()
{
   string str1 = "runoops";
   string str2 = "google";
   string str3;
   int  len ;
 
   // 复制 str1 到 str3
   str3 = str1;
   cout << "str3 : " << str3 << endl;
 
   // 连接 str1 和 str2
   str3 = str1 + str2;
   cout << "str1 + str2 : " << str3 << endl;
 
   // 连接后,str3 的总长度
   len = str3.size();
   cout << "str3.size() :  " << len << endl;
 
   return 0;
}

当上面的代码被编译和执行时,它会产生下列结果:

str3 : runoops
str1 + str2 : runoopsgoogle
str3.size() :  13

string类提供了一系列针对字符串的操作,比如:

  •  1. append() -- 在字符串的末尾添加字符
  •  2. find() -- 在字符串中查找字符串
  •  4. insert() -- 插入字符
  •  5. length() -- 返回字符串的长度
  •  6. replace() -- 替换字符串
  •  7. substr() -- 返回某个子字符串
  •  8. ...

下面是关于string类的实例:

实例

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

int main()
{
    //定义一个string类对象
    string http = "www.runoops.com";

   //打印字符串长度
   cout<<http.length()<<endl;

    //拼接
    http.append("/C++");
    cout<<http<<endl; //打印结果为:www.runoops.com/C++

    //删除
    int pos = http.find("/C++"); //查找"C++"在字符串中的位置
    cout<<pos<<endl;
    http.replace(pos, 4, "");   //从位置pos开始,之后的4个字符替换为空,即删除
    cout<<http<<endl;

    //找子串runoops
    int first = http.find_first_of("."); //从头开始寻找字符'.'的位置
    int last = http.find_last_of(".");   //从尾开始寻找字符'.'的位置
    cout<<http.substr(first+1, last-first-1)<<endl; //提取"runoops"子串并打印

    return 0;
}

当上面的代码被编译和执行时,它会产生下列结果:

15
www.runoops.com/C++
15
www.runoops.com
runoops