Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

C# 字符串连接


字符串连接

可以使用 + 运算符连接两个字符串。这被称为 **连接**

例子

string firstName = "John ";
string lastName = "Doe";
string name = firstName + lastName;
Console.WriteLine(name);

自己尝试 »

请注意,我们在 "John" 后添加了一个空格,以便在打印时在 firstName 和 lastName 之间创建空格。

您也可以使用 string.Concat() 方法连接两个字符串

例子

string firstName = "John ";
string lastName = "Doe";
string name = string.Concat(firstName, lastName);
Console.WriteLine(name);

自己尝试 »


添加数字和字符串

警告!

C# 使用 + 运算符进行加法和连接。

**记住:**数字相加。字符串连接。

如果添加两个数字,结果将是一个数字

例子

int x = 10;
int y = 20;
int z = x + y;  // z will be 30 (an integer/number)

自己尝试 »

如果添加两个字符串,结果将是一个字符串连接

例子

string x = "10";
string y = "20";
string z = x + y;  // z will be 1020 (a string)

自己尝试 »


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.