ตั้งสตริงที่มีคำซ้ำกัน
string str = "One Two Three One";
ด้านบนคุณจะเห็นคำว่า "หนึ่ง" มาสองครั้ง
หากต้องการลบคำที่ซ้ำกัน คุณสามารถลองใช้รหัสต่อไปนี้ใน C# -
ตัวอย่าง
using System;
using System.Linq;
public class Program {
public static void Main() {
string str = "One Two Three One";
string[] arr = str.Split(' ');
Console.WriteLine(str);
var a =
from k in arr
orderby k
select k;
Console.WriteLine("After removing duplicate words...");
foreach(string res in a.Distinct()) {
Console.Write(" " + res.ToLower());
}
Console.ReadLine();
}
} ผลลัพธ์
One Two Three One After removing duplicate words... one three two