วิธีการเข้าร่วม () ในสตริงเชื่อมองค์ประกอบทั้งหมดของอาร์เรย์สตริงโดยใช้ตัวคั่นที่ระบุระหว่างแต่ละองค์ประกอบ
ในตัวอย่างด้านล่าง เรามีสตริงหลายบรรทัด และเราได้ตั้งค่าตัวคั่นเป็น “\n” −
String.Join("\n", starray); ตัวอย่าง
ต่อไปนี้เป็นตัวอย่างที่สมบูรณ์ −
using System;
namespace StringApplication {
class StringProg {
static void Main(string[] args) {
string[] starray = new string[]{"Down the way nights are dark",
"And the sun shines daily on the mountaintop",
"I took a trip on a sailing ship",
"And when I reached Jamaica",
"I made a stop"};
string str = String.Join("\n", starray);
Console.WriteLine(str);
}
}
} ผลลัพธ์
Down the way nights are dark And the sun shines daily on the mountain top I took a trip on a sailing ship And when I reached Jamaica I made a stop