最适合网络开发者的网站
C++ 语言。W3Schools 英文版。初学者课程

尿素 西斯

C++ String Concatenation


String Concatenation

The + operator can be used between strings to add them together to make a new string. This is called concatenation:

例子

string firstName = "John ";
string lastName = "Doe";
string fullName = firstName + lastName;
cout << fullName;
Try it Yourself »

In the example above, we added a space after firstName to create a space between John and Doe on output. However, you could also add a space with quotes (" " or ' '):

例子

string firstName = "John";
string lastName = "Doe";
string fullName = firstName + " " + lastName;
cout << fullName;
Try it Yourself »

Append

A string in C++ is actually an object, which contains functions that can perform certain operations on strings. For example, you can also concatenate strings with the append() function:

例子

string firstName = "John ";
string lastName = "Doe";
string fullName = firstName.append(lastName);
cout << fullName;
Try it Yourself »


评论