Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

วิธีแบ่งสตริงเป็นสมาชิกอาร์เรย์สตริงใน C#


กำหนดสตริงที่ต้องการแบ่ง

string str = "Hello World!";

ใช้เมธอด Split() เพื่อแบ่งสตริงเป็นสมาชิกแยกกัน

string[] res = str.Split(' ');

โค้ดเต็มสำหรับแบ่งสตริงเป็นสมาชิกอาร์เรย์สตริงใน C#

ตัวอย่าง

using System; 
class Demo { 
    static void Main() { 
        string str = "Hello World!"; 
        string[] res = str.Split(' '); 
        Console.WriteLine("Separate elements:"); 
        foreach (string words in res) { 
            Console.WriteLine(words); 
        } 
    } 
}

ผลลัพธ์

Separate elements: 
Hello 
World!