c#中concat函数的用法是什么

c#
1166
2024/3/7 16:56:52
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在C#中,Concat()函数用于连接两个或多个字符串,并返回一个新的字符串。该函数可以接受任意数量的字符串作为参数,将它们按照参数的顺序连接起来。下面是Concat()函数的用法示例:

string str1 = "Hello";
string str2 = "World";
string result = string.Concat(str1, str2);

Console.WriteLine(result); // 输出结果为 "HelloWorld"

除了连接字符串之外,Concat()函数还可以连接其他类型的对象,它会将这些对象的ToString()方法返回的结果转换为字符串并连接起来。例如:

int num1 = 10;
double num2 = 20.5;
string result = string.Concat("The numbers are: ", num1, " and ", num2);

Console.WriteLine(result); // 输出结果为 "The numbers are: 10 and 20.5"

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: C#中怎么使用DateTime.Compare()比较时间大小