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.");

   //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?

使用 '+' 运算符进行字符串连接

添加字符串最简单的方法之一是在两个或多个字符串上使用 '+' 运算符。这可以在原地(即,无需创建新字符串)或在新的字符串对象中完成。这是 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;
   //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? 

使用 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];
   }
   //using for loop to iterate over new_string

   cout << initial_string << endl;
   return 0;
}

输出

I Love TP. I am new here.

使用 while 循环计算字符串长度

我们还可以使用一个简单的 while 循环。此循环运行直到我们到达字符串的末尾,并且在每次迭代中,我们可以将相应的字符添加到初始字符串中。这可以在原地完成,也可以使用新的 字符串对象 完成。这种类型的连接在 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++;
   }
   //using while loop to iterate over 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;
   }
   //using range based loop for concatentation

   cout << initial_string << endl;
   return 0;
}

输出

I Love TP. I am new here.

使用 strcat() 函数进行字符串连接

我们可以使用 strcat() 函数在 C++ 中连接字符串。但是,此方法不适用于字符串对象,它仅适用于 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? ";

   //using strcat function to concatenate
   //result stored in s1

   strcat(s1,s2);
   cout << s1 << endl;
   return 0;
}

输出

I love  TP. Could you help me? 

使用继承进行字符串连接

我们可以使用 strcat() 函数在 C++ 中连接字符串。但是,此方法不适用于字符串对象,它仅适用于 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;
      //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 !!!

使用 OOPS 中的友元函数进行字符串连接

友元类可以访问在其声明为友元的其他类的私有和受保护成员。有时允许特定类访问其他类的私有和受保护成员非常有用。

因此,我们利用此友元类来声明一个辅助方法,然后对 C 风格字符串使用 strcat() 函数。

语法

以下语法用于在 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; 

// 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 !!!
广告
© . All rights reserved.