Cplusplus 简明教程
C++ String Concatenation
字符串连接是向现有元素添加元素的过程。在此上下文中,字符串连接是可以将两个(或更多)字符串添加到一个字符串上的方法。因此,最终的字符串是初始字符串和附加字符串的组合。
String concatenation is the process of adding an element to an existing element. In this context, string concatenation is the method by which two (or more) strings can be added to one another. Hence, the resultant string is the combination of the initial string and the added string.
在 C++ 中连接字符串有几种方法,其中一些如下所示 −
There are several methods to concatenate strings in C++, some of which are given as follows −
-
Using string::append() function
-
Using '+' operator
-
Using strcat() function for C-style strings
-
Using for loop
-
Using while loop
-
Using range based loop
-
Using inheritance
-
Using friend function with OOPS
这些方法将在本章的下一篇几篇文章中详细解释。所以,让我们深入研究这些概念。
These methods are explained in detail in the next few articles of this chapter. So, let’s dive into these concepts.
String Concatenation Using string::append() Function
String is a class defined in <string> header file, and the append() function is an inherited method of that class. This method is used to append or add a given string to an initial string.
Syntax
使用 append() 方法连接字符串的语法如下 −
The following syntax is used to concatenate string using the append() method −
initial_string.append(new_string);
initial_string.append(“this is new”);
Parameters
string::append() 函数将字符串作为参数。字符串可以显式传递,也可以作为对象传递。
The string::append() function takes a string as a parameter. The string can be passed explicitly or as an object.
Example of append() function
以下示例代码用于使用 append() 方法连接字符串 −
The following exemplar code is used to concatenate string using the append() method −
#include <iostream>
using namespace std;
int main() {
string initial_string("I Love TP.");
string new_string(" I am new here.");
//using append function with object parameter
initial_string.append(new_string);
//using append function with explicit 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?
String Concatenation Using '+' Operator
添加字符串最简单的方法之一是在两个或更多个字符串上使用 '' operator 。这可以就地完成(即不创建新字符串)或在新字符串对象中完成。这是 C+ 编程语言的 newer features 之一。
One of the easiest way to add strings is to use the '' operator on two or more strings. This can be done in place (i.e. without creating a new string), or in a new string object. This is one of the newer features of C+ programming language.
Syntax
通过使用 + 运算符连接字符串的语法如下 −
The following syntax is used to concatenate string using the '+' opertaor −
new_string=initial_string+added_string;
initial_string+=added_string
此处,可以在原地或通过创建新的字符串对象添加新的字符串。
Here, the new string can be added in place or by creating a new string object.
Example
通过使用 + 运算符连接字符串的示例代码如下 −
The following exemplar code is used to concatenate string using the '+' opertaor −
#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;
//using new string object
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?
String Concatenation Using for Loop
我们可以从新字符串的开头到新字符串的末尾使用一个简单的 for loop ,并且在每一次迭代中,我们可以将该字符添加到初始字符串中。这可以在原地进行,或者使用新的字符串对象。也可以在 C-style strings 中进行这种连接,它们是字符数组。
We can use a simple for loop from the beginning of the new string to the end of the new string, and for each iteration, we can add that character to the initial string. This can be done in place, or by using a new string object. This type of concatenation is also possible in C-style strings, which are character arrays.
Syntax
通过使用从字符串开头到结尾的 for 循环连接字符串的语法如下 −
The following syntax is used to concatenate string using for loop from beginning of the string to the end −
for(int i=0;i<s.length();i++){
initial+=s[i];
}
Example
通过使用从字符串开头到结尾的 for 循环连接字符串的示例代码如下 −
The following exemplar code is used to concatenate string using for loop from beginning of the string to the end −
#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];
}
//using for loop to iterate over new_string
cout << initial_string << endl;
return 0;
}
I Love TP. I am new here.
String Length Using while Loop
我们也可以使用一个简单的 while loop 。此循环一直运行到我们到达字符串的末尾,在每一次迭代中,我们可以将相应字符添加到初始字符串中。这可以在原地进行,或者使用新的 string object 。也可以在 C-style strings 中进行这种连接,它们是字符数组。
We can also use a simple while loop. This loop runs till we reach the end of the string, and at each iteration, we can add the corresponding character to the initial string. This can be done in place, or by using a new string object. This type of concatenation is also possible in C-style strings, which are character arrays.
Syntax
通过使用从字符串开头到结尾的 while 循环连接字符串的语法如下 −
The following syntax is used to concatenate string using while loop from beginning of the string to the end −
for(int i=0;i<s.length();i++){
initial+=s[i];
}
Example
通过使用从字符串开头到结尾的 while 循环连接字符串的示例代码如下 −
The following exemplar code is used to concatenate string using while loop from beginning of the string to the end −
#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++;
}
//using while loop to iterate over new_string
cout << initial_string << endl;
return 0;
}
String Concatenation Using range based loop
我们还可以使用基于范围的循环,这些循环会自动迭代整个字符串,并且我们可以将每个字符添加到初始字符串中。这可以在原地进行,或者使用新的字符串对象。
We can also use a range based loop, which will automatically iterate over the whole string and we can add each character to the initial string. This can be done in place, or by using a new string object.
Syntax
通过使用从字符串开头到结尾的基于范围的循环连接字符串的语法如下 −
The following syntax is used to concatenate string using range based loop from beginning of the string to the end −
for(char c: s){
initial+=c;
}
Example
通过使用从字符串开头到结尾的基于范围的循环连接字符串的示例代码如下 −
The following exemplar code is used to concatenate string using range based loop from beginning of the string to the end −
#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;
}
//using range based loop for concatentation
cout << initial_string << endl;
return 0;
}
I Love TP. I am new here.
String Concatenation Using strcat() Function
我们可以使用 strcat() 函数连接 C++ 中的字符串。不过,此方法不适用于字符串对象,只适用于 C 式字符串,即字符数组。此方法在 <string.h>头文件中定义。
We can use strcat() function to concatenate strings in C++. But, this method does not work for string objects, it only works for C-style strings, i.e. character arrays. This method is defined in the <string.h> header file.
Syntax
通过使用 strcat() 方法连接字符串的语法如下 −
The following syntax is used to concatenate string using the strcat() method −
strcat(s1,s2);
Parameters
此处,s1 和 s2 是两个字符数组(即字符串),它们作为参数传递给 strcat() 方法。
Here, s1 and s2 are two character arrays (i.e. strings) which are passed as parameters to the strcat() method.
Example
通过使用 strcat() 方法连接字符串的示例代码如下 −
The following exemplar code is used to concatenate string using the strcat() method −
#include <bits/stdc++.h>
using namespace std;
int main(){
char s1[]="I love ";
char s2[]=" TP. Could you help me? ";
//using strcat function to concatenate
//result stored in s1
strcat(s1,s2);
cout << s1 << endl;
return 0;
}
I love TP. Could you help me?
String Concatenation Using Inheritance
我们可以使用 strcat() 函数连接 C++ 中的字符串。不过,此方法不适用于字符串对象,只适用于 C 式字符串,即字符数组。此方法在 <string.h>头文件中定义。
We can use strcat() function to concatenate strings in C++. But, this method does not work for string objects, it only works for C-style strings, i.e. character arrays. This method is defined in the <string.h> header file.
Syntax
通过使用面向对象编程概念中的继承方法连接字符串的语法如下 −
The following syntax is used to concatenate string using the inheritance method in OOP concepts −
strcat(s1,s2);
Example
通过使用面向对象编程概念中的继承方法连接字符串的示例代码如下 −
The following exemplar code is used to concatenate string using the inheritance method in OOP concepts −
#include <bits/stdc++.h>
using namespace std;
//parent class
class parent {
public:
virtual string concatenate(string s1, string s2) = 0;
//creating a virtual method to inherit
};
//child class
class child: parent {
public:
string concatenate (string s1, string s2){
s1+=s2;
//using + operator to add strings
return s1;
}
};
int main() {
child ch1;
cout << ch1.concatenate ("I love ", "TP !!!");
return 0;
}
I love TP !!!
String Concatenation Using Friend Function with OOPS
友元类可以访问它被声明为友元的其他类的私有成员和受保护成员。有时允许特定类访问其他类的私有成员和受保护成员很有用。
A friend class can access private and protected members of other classes in which it is declared as a friend. It is sometimes useful to allow a particular class to access private and protected members of other classes.
因此,我们使用此友元类来声明一个辅助方法,然后对 C 样式字符串使用 strcat() 函数。
Hence, we make use of this friend class to declare a helper method, and then use the strcat() function for C-style strings.
Syntax
使用 C++ 中的友元方法连接字符串时使用以下语法:
The following syntax is used to concatenate string using the friend method in C++ −
Class A {
string s1=[value];
string s2=[value];
friend void helper(A obj);
}
helper(A) {
strcat(obj.s1, obj.s2);
};
Example
连接使用 C++ 中的友元方法的示例如下:
The following exemplar code is used to concatenate string using the friend method in C++ −
#include <bits/stdc++.h>
using namespace std;
// concatenate class
class concatenate {
public:
char s1[20] = "I love TP !!!";
char s2[20] = "Hey... ";
friend void helper(concatenate par1);
};
void helper (concatenate par1) {
// Pass parameter to concatenate
strcat (par1.s2, par1.s1);
cout << par1.s2;
}
int main() {
// Create object of class
concatenate par1;
//pass this object to helper function
helper(par1);
return 0;
}
Hey... I love TP !!!