C ++字符串类及其应用程序?

C ++具有String类。这与传统的C字符串不同。C字符串实际上是字符数组。在C ++中,字符串类具有几个不同的属性。它具有不同的功能,可用于执行不同的任务。在这里,我们将看到String类的重要功能。

在第一部分中,我们将看到字符串类的构造函数如何以不同的方式工作。让我们以身作则。

示例

#include<iostream>
using namespace std;
int main() {
   string str("This is a string");
   cout << "String is: " << str << endl;
   string str2(str); // 由另一个字符串str初始化
   cout << "String is: " << str2 << endl;
   string str3(5, 'A'); //用字符初始化
   cout << "String is: "<< str3 << endl;
   string str4(str, 5, 10); //使用从索引5到索引10的另一个字符串进行初始化
   cout << "String is: " << str4 << endl;
   string str5(str.begin(), str.begin() + 7); //使用str从first到index 7进行初始化
   cout << "String is: " << str5 << endl;
}

输出结果

String is: This is a string
String is: This is a string
String is: AAAAA
String is: is a strin
String is: This is

现在让我们讨论一些String类的运算符。基本运算符是赋值运算符(=),串联运算符(+),索引运算符([])。让我们看看这些运算符的示例。

示例

#include<iostream>
using namespace std;
int main() {
   string str = "Hello ";
   string str2 = "World";
   string str3, str4;
   str3 = str; //use assignment operator
   cout << "The value of str3: " << str3 << endl;
   str4 = str + str2; //concatenate two strings
   cout << "The value of str4: " << str4 << endl;
   cout << "Character at position 1 of str: " << str[1] << endl;
}

输出结果

The value of str3: Hello
The value of str4: Hello World
Character at position 1 of str: e

最后给我们看一些字符串函数。这些函数用于完成一些与字符串有关的重要任务。让我们看看示例代码中的功能

示例

#include<iostream>
using namespace std;
int main() {
   string str = "Hello";
   cout << "String before clear: " << str << endl;
   str.clear(); //clean the string
   cout << "String after clear: " << str << endl;
   str = "这是一个字符串:";
   cout << "String length using length() and size() functions: " <<str.length() << " and " << str.size() << endl;
   cout << "Character at position 1 of str: " << str.at(1) << endl;
   cout << "First character of str: " << str.front() << endl;
   cout << "Last character of str: " << str.back() << endl;
   cout << "String to C like character array: " << str.c_str() << endl;
   cout << "String after appending text : " << str.append("ABCDEF") << endl;
   string str2 = "ANOTHER STRING";
   cout << "String after appending text from str2 : " << str.append(str2,0, 5) << endl;
   //查找功能以查找子字符串
   if (str.find("is") != string::npos)
      cout << "\"is\" is found in str at " << str.find("is") << " pos" <<endl;
   else
      cout << "substring not found in str" << endl;
   cout << "Substring of length 3 from index 5: " <<str.substr(5, 3) <<endl;
   cout << "String after erasing 4 characters from index 5: " <<str.erase(5, 4) << endl;
   cout << "Replace 7 characters from index 3: " <<str.replace(3, 7, "C++Programming");
}

输出结果

String before clear: Hello
String after clear:
String length using length() and size() functions: 17 and 17
Character at position 1 of str: h
First character of str: T
Last character of str: :
String to C like character array: 这是一个字符串:
String after appending text : 这是一个字符串:ABCDEF
String after appending text from str2 : 这是一个字符串:ABCDEFANOTH
"is" is found in str at 2 pos
Substring of length 3 from index 5: is
String after erasing 4 characters from index 5: This
string:ABCDEFANOTH
Replace 7 characters from index 3: ThiC++ Programmingng:ABCDEFANOTH