string.Concat方法用于连接string的一个或多个实例,或string的一个或多个实例的object表示形式

https://docs.microsoft.com/zh-cn/dotnet/api/system.string.concat?view=net-5.0
1)连接string[]或object[]数组中的元素
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static object[] strArr = new object[5];
//static string[] strArr = new string[5]; //等价于上一句
static void Main(string[] args)
{
for (int i = 0; i < 5; i++)
{
strArr[i] = i.ToString() + ' ';
}
string str = string.Concat(strArr);
Console.WriteLine(str);
}
}
}
运行结果如下:

2)连接多个string实例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static string str1 = "Hello ";
static string str2 = "World!";
static void Main(string[] args)
{
string str3 = string.Concat(str1, str2);
Console.WriteLine(str3);
}
}
}
运行结果如下:

本文介绍了C#中的string.Concat方法,用于连接一个或多个string实例或object数组中的元素,详细阐述了如何使用该方法进行字符串拼接,并展示了实际运行结果。
1607

被折叠的 条评论
为什么被折叠?



