ขั้นแรก ตั้งค่าสตริง −
string str = "Science and Mathematics";
ตอนนี้ใช้วิธี Split() เพื่อแยกทุกที่ที่มีช่องว่าง -
str.Split(' ') ต่อไปนี้เป็นรหัสที่สมบูรณ์ -
ตัวอย่าง
using System;
using System.Linq;
using System.IO;
class Program {
static void Main() {
string str = "Science and Mathematics";
Console.WriteLine("String...\n"+str);
string[] myStr = str.Split(' ');
Console.WriteLine("\nSplitted String...");
foreach (string ch in myStr) {
Console.WriteLine(ch);
}
}
} ผลลัพธ์
String... Science and Mathematics Splitted String... Science and Mathematics