C++ 字符串连接
字符串连接是将一个元素添加到现有元素的过程。在本文中,字符串连接是指将两个(或多个)字符串相加的方法。因此,结果字符串是初始字符串和添加字符串的组合。
C++ 中有几种连接字符串的方法,其中一些方法如下:
- 使用 string::append() 函数
- 使用 '+' 运算符
- 使用 strcat() 函数 处理 C 风格的字符串
- 使用 for 循环
- 使用 while 循环
- 使用 基于范围的 循环
- 使用 继承
- 在 OOPS 中使用 友元函数
本章接下来的几篇文章将详细解释这些方法。那么,让我们深入探讨这些概念。
使用 string::append() 函数连接字符串
String 是定义在 <string> 头文件中的类,而 append() 函数是该类的继承方法。此方法用于将给定字符串附加到初始字符串。
语法
以下语法用于使用 append() 方法连接字符串 -
initial_string.append(new_string); initial_string.append(this is new);
参数
string::append() 函数接受一个字符串作为参数。该字符串可以显式传递,也可以作为对象传递。
append() 函数示例
以下示例代码用于使用 append() 方法连接字符串 -
#include <iostream>
using namespace std;
int main() {
string initial_string("I Love TP.");
string new_string(" I am new here.");
//使用带有对象参数的 append 函数
initial_string.append(new_string);
//使用带有显式字符串的附加函数
initial_string.append(" Could you help me?");
cout << initial_string << endl;
return 0;
}
输出
I Love TP. I am new here. Could you help me?
使用"+"运算符连接字符串
添加字符串最简单的方法之一是对两个或多个字符串使用"+"运算符。这可以在原地完成(即无需创建新字符串),也可以在新的字符串对象中完成。这是 C++ 编程语言的新功能之一。
语法
以下语法用于使用"+"运算符连接字符串 -
new_string=initial_string+added_string; initial_string+=added_string
在这里,可以就地添加新字符串,也可以通过创建新的字符串对象来添加。
示例
以下示例代码用于使用"+"运算符连接字符串 -
#include <iostream>
using namespace std;
int main() {
string initial_string("I Love TP.");
string new_string(" I am new here.");
string a="Hey !!! " + initial_string;
//使用新的字符串对象
a+=new_string;
//inplace addition
a+=" Could you help me? ";
cout << a << endl;
return 0;
}
输出
Hey !!! I Love TP. I am new here. Could you help me?
使用 for 循环进行字符串连接
我们可以使用一个简单的 for 循环,从新字符串的开头到结尾进行连接,每次迭代时,我们将该字符添加到初始字符串中。这可以在原地完成,也可以通过使用新的字符串对象来完成。这种类型的连接也可以在 C 风格字符串(即字符数组)中进行。
语法
以下语法用于使用 for 循环从字符串的开头到结尾连接字符串 -
for(int i=0;i<s.length();i++){
initial+=s[i];
}
示例
以下示例代码用于使用 for 循环从字符串开头到结尾连接字符串 -
#include <iostream>
using namespace std;
int main(){
string initial_string("I Love TP.");
string new_string(" I am new here.");
for(int i=0;i<new_string.size();i++){
initial_string+=new_string[i];
}
//使用 for 循环遍历 new_string
cout << initial_string << endl;
return 0;
}
输出
I Love TP. I am new here.
使用 while 循环计算字符串长度
我们也可以使用一个简单的 while 循环。此循环一直运行到字符串末尾,每次迭代时,我们都可以将相应的字符添加到初始字符串中。这可以就地完成,也可以通过使用新的 string 对象 来完成。这种类型的连接也可以在 C 风格字符串(即字符数组)中进行。
语法
以下语法用于使用 while 循环从字符串开头到结尾连接字符串 -
for(int i=0;i<s.length();i++){
initial+=s[i];
}
示例
以下示例代码用于使用 while 循环从字符串开头到结尾连接字符串 -
#include <iostream>
using namespace std;
int main() {
string initial_string("I Love TP.");
string new_string(" I am new here.");
int i=0;
while(new_string[i]!='\0'){
initial_string+=new_string[i];
i++;
}
//使用 while 循环迭代 new_string
cout << initial_string << endl;
return 0;
}
使用基于范围的循环进行字符串连接
我们还可以使用基于范围的循环,它会自动遍历整个字符串,并将每个字符添加到初始字符串中。这可以就地完成,也可以通过使用新的字符串对象来完成。
语法
以下语法用于使用基于范围的循环从字符串的开头到结尾连接字符串 -
for(char c: s){
initial+=c;
}
示例
以下示例代码用于使用基于范围的循环从字符串的开头到结尾连接字符串 -
#include <iostream>
using namespace std;
int main() {
string initial_string("I Love TP.");
string new_string(" I am new here.");
for(char c: new_string){
initial_string+=c;
}
//使用基于范围的循环进行连接
cout << initial_string << endl;
return 0;
}
输出
I Love TP. I am new here.
使用 strcat() 函数进行字符串连接
在 C++ 中,我们可以使用 strcat() 函数连接字符串。但是,此方法不适用于字符串对象,仅适用于 C 风格的字符串,即字符数组。此方法定义在 <string.h> 头文件中。
语法
使用 strcat() 方法连接字符串的语法如下:
strcat(s1,s2);
参数
此处,s1 和 s2 是两个字符数组(即字符串),它们作为参数传递给 strcat() 方法。
示例
以下示例代码用于使用 strcat() 方法连接字符串 -
#include <bits/stdc++.h>
using namespace std;
int main(){
char s1[]="I love ";
char s2[]=" TP. Could you help me? ";
//使用 strcat 函数进行连接
//结果存储在 s1 中
strcat(s1,s2);
cout << s1 << endl;
return 0;
}
输出
I love TP. Could you help me?
使用继承进行字符串连接
在 C++ 中,我们可以使用 strcat() 函数连接字符串。但是,此方法不适用于字符串对象,它仅适用于 C 风格的字符串,即字符数组。此方法定义在 <string.h> 头文件中。
语法
以下语法用于在 OOP 概念中使用继承方法连接字符串 -
strcat(s1,s2);
示例
以下示例代码用于在 OOP 概念中使用继承方法连接字符串 -
#include <bits/stdc++.h>
using namespace std;
//parent class
class parent {
public:
virtual string concatenate(string s1, string s2) = 0;
//创建要继承的虚方法
};
//子类
class child: parent {
public:
string concatenate (string s1, string s2){
s1+=s2;
//使用 + 运算符添加字符串
return s1;
}
};
int main() {
child ch1;
cout << ch1.concatenate ("I love ", "TP !!!");
return 0;
}
输出
I love TP !!!
使用友元函数进行字符串连接,并实现面向对象编程 (OOPS)
友元类可以访问其他类的私有和受保护成员,只要它被声明为友元类。有时,允许特定类访问其他类的私有和受保护成员会很有用。
因此,我们利用这个友元类声明一个辅助方法,然后使用 strcat() 函数来处理 C 风格的字符串。
语法
以下语法用于在 C++ 中使用友元方法连接字符串 -
Class A {
string s1=[value];
string s2=[value];
friend void helper(A obj);
}
helper(A) {
strcat(obj.s1, obj.s2);
};
示例
以下示例代码用于在 C++ 中使用友元方法连接字符串 -
#include <bits/stdc++.h>
using namespace std;
// 连接类
class concatenate {
public:
char s1[20] = "I love TP !!!";
char s2[20] = "Hey... ";
friend void helper(concatenate par1);
};
void helper (concatenate par1) {
// 传递参数进行连接
strcat (par1.s2, par1.s1);
cout << par1.s2;
}
int main() {
// 创建类的对象
concatenate par1;
//将此对象传递给辅助函数
helper(par1);
return 0;
}
输出
Hey... I love TP !!!

