C# String Concat() 字符串拼接方法
String的Concat()
方法用于连接字符串并返回结果。
示例
using System;
namespace CsharpString {
class Test {
public static void Main(string [] args) {
string str0 = "Ice";
string str1 = " cream";
// 连接str0和str1
string result = String.Concat(str0 + str1);
Console.WriteLine(result);
Console.ReadLine();
}
}
}
// 输出: Ice cream
Concat() 语法
String的Concat()
方法的语法如下:
String.Concat(string str0, string str1)
在这里,Concat()
是String类的一个方法。
Concat() 参数
Concat()
方法接受以下参数:
- str0 - 要连接的第一个字符串
- str1 - 要连接的第二个字符串
Concat() 返回值
Concat()
方法返回:
- 一个字符串,即
str0
和str1
的连接结果
示例 1:C# String Concat()
using System;
namespace CsharpString {
class Test {
public static void Main(string [] args) {
string str0 ="ice";
string str1 = "cream";
string result;
// 连接str0和str1
result = String.Concat(str0, str1);
Console.WriteLine(result);
// 连接str0和str1
result = String.Concat(str1, str0);
Console.WriteLine(result);
Console.ReadLine();
}
}
}
输出
icecream
creamice
在上面的示例中,
String.Concat(str0, str1);
将str1
连接到str0
的末尾String.Concat(str1, str0);
将str0
连接到str1
的末尾
示例 2:Concat() - 连接三个字符 串
using System;
namespace CsharpString {
class Test {
public static void Main(string [] args) {
string str0 = "Everyone";
string str1 = " loves";
string str2 = " ice cream";
// 连接str0、str1和str2
string result = String.Concat(str0, str1, str2);
Console.WriteLine(result);
Console.ReadLine();
}
}
}
输出
Everyone loves ice cream
在上面的示例中,Concat()
方法连接了三个字符串。
示例 3:Concat() - 数组元素
using System;
namespace CsharpString {
class Test {
public static void Main(string [] args) {
string[] str = {"Chocolate", " Ice cream", " Vanilla"};
// 连接字符串数组
string result = String.Concat(str);
Console.WriteLine(result);
Console.ReadLine();
}
}
}
输出
Chocolate Ice cream Vanilla
在上面的示例中,Concat()
方法连接了数组str
中的元素。