1、C 风格字符串
C 风格字符串是使用\0终止的一维字符数组。'\0'就是 ASCII 码为 0 的字符,对应的字符是(Null),表示"字符串结束符",是字符串结束的标志。由于在数组的末尾存储了空字符,所以字符数组的大小比字符数多一个。本质是字符数组,手动管理内存。通常需要 #include <cstring> 进行操作。
char name[8] = {'C', 'J', 'A', 'V', 'A', 'P','Y', '\0'};
也可以直接使用字符串初始化:
char name[] = "CJAVAPY";
需要把 null 字符(\0)放在字符串常量的末尾。编译器会在初始化数组时,自动把 \0 放在字符串的末尾。
例如,
#include <iostream>
using namespace std;
#include <cstring> // 提供字符串操作函数
int main() {
    char str1[20] = "Hello";
    char str2[20] = "World";
    // 拼接字符串
    strcat(str1, str2);
    std::cout << "拼接后的字符串: " << str1 << std::endl;
    // 比较字符串
    if (strcmp(str1, str2) == 0) {
        std::cout << "两个字符串相等" << std::endl;
    } else {
        std::cout << "两个字符串不相等" << std::endl;
    }
    return 0;
}
注意:#include <string.h> 来源于 C 语言,主要用于 C 风格字符串操作,提供的函数包括 strlen, strcpy, strcat, strcmp 等。在 C++ 中兼容,但不属于标准的 C++ 命名空间。#include <cstring>属于 C++ 标准库,将 C 风格字符串操作函数引入到 std 命名空间中。推荐在 C++ 程序中使用 #include <cstring>,以保持代码的现代性和一致性。
2、字符串的函数
需要 #include <cstring>,操作的是 char[] 类型的字符串。
| 函数 | 功能描述 | 
|---|---|
| strcpy(s1, s2) | 复制字符串 s2到字符串 s1。 | 
| strcat(s1, s2) | 连接字符串 s2到字符串 s1的末尾。 | 
| strlen(s1) | 返回字符串 s1的长度。 | 
| strcmp(s1, s2) | 如果 s1和s2相同,返回 0;如果 s1 < s2,返回小于 0; 如果 s1 > s2,返回大于 0。 | 
| strchr(s1, ch) | 返回一个指针,指向字符串 s1中字符 ch的第一次出现的位置。 | 
| strstr(s1, s2) | 返回一个指针,指向字符串 s1中字符串 s2的第一次出现的位置。 | 
例如,
#include <iostream>
using namespace std;
#include <cstring> // 提供字符串操作函数
int main ()
{
   char str1[14] = "cjavapy";
   char str2[14] = "csharp";
   char str3[14];
   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;
}3、String 类字符串
C++ 标准库提供了 string 类类型,支持上述所有的操作,另外还增加了其他更多的功能。string是C++标准库的一个重要的部分,主要用于字符串处理。可以使用输入输出流方式直接进行string操作,也可以通过文件等手段进行string操作。同时,C++的算法库对string类也有着很好的支持,并且string类还和c语言的字符串之间有着良好的接口。
例如,
1)string转换为char*
#include <string>
#include <iostream>
 
using namespace std;
 
int main()
{
    string strOutput = "Hello World";
 
    cout << "[cout] strOutput is: " << strOutput << endl;
 
    // string 转换为 char*
    const char* pszOutput = strOutput.c_str();
    
    printf("[printf] strOutput is: %s\n", pszOutput);
 
    return 0;
}
2)string方法示例
#include <string>
#include <iostream>
#define HELLOSTR "Hello World"
using namespace std;
 
int main()
{
    string strOutput = "Hello World";
 
    int nLen = strOutput.length();
 
    cout << "the length of strOutput is: " << nLen << endl;
 
    if (0 == strOutput.compare(HELLOSTR))
    {
    cout << "strOutput equal with macro HELLOSTR" << endl;
    }
 
    return 0;
}
4、C++ std::string 成员函数
std::string 提供了更现代化、便捷的字符串操作方法。
| 函数 | 功能描述 | 
|---|---|
| .length()/.size() | 返回字符串长度 | 
| .empty() | 判断字符串是否为空 | 
| .clear() | 清空字符串 | 
| .append() | 追加字符串 | 
| .insert() | 在指定位置插入字符串 | 
| .erase() | 删除指定范围的字符 | 
| .replace() | 替换指定范围的字符 | 
| .substr() | 获取子字符串 | 
| .find() | 查找子字符串的位置 | 
| .rfind() | 从后往前查找子字符串的位置 | 
| .compare() | 比较两个字符串 | 
| .c_str() | 返回 C 风格字符串指针 | 
#include <iostream>
#include <string>
int main() {
    std::string str = "Hello";
    // 追加字符串
    str.append(" World");
    std::cout << "拼接后的字符串: " << str << std::endl;
    // 获取子字符串
    std::string sub = str.substr(0, 5);
    std::cout << "子字符串: " << sub << std::endl;
    // 替换字符串
    str.replace(0, 5, "Hi");
    std::cout << "替换后的字符串: " << str << std::endl;
    // 查找子字符串
    size_t pos = str.find("World");
    if (pos != std::string::npos) {
        std::cout << "找到子字符串 'World',位置: " << pos << std::endl;
    } else {
        std::cout << "未找到子字符串 'World'" << std::endl;
    }
    return 0;
}5、非成员函数(C++ 标准库支持)
 
| 函数 | 功能描述 | 
|---|---|
| std::getline | 从输入流中读取一整行字符串 | 
| std::to_string | 将数值转换为字符串 | 
| std::stoi | 将字符串转换为整数 | 
| std::stof | 将字符串转换为浮点数 | 
#include <iostream>
#include <string>
int main() {
    int num = 42;
    std::string str = std::to_string(num);
    std::cout << "数值转换为字符串: " << str << std::endl;
    std::string input;
    std::cout << "输入一行文字: ";
    std::getline(std::cin, input);
    std::cout << "你输入的内容是: " << input << std::endl;
    return 0;
}