欧美性猛交xxx嘿人猛交_又色又爽又高潮免费观看_精品国产一区二区三区久久影院_青娱乐极品视觉盛宴国产视频

技術頻道導航
HTML/CSS
.NET技術
IIS技術
PHP技術
Js/JQuery
Photoshop
Fireworks
服務器技術
操作系統
網站運營

贊助商

分類目錄

贊助商

最新文章

搜索

C#從字符串中刪除重復的單詞的兩種方法

作者:admin    時間:2023-5-1 1:20:8    瀏覽:

本文介紹C#從字符串中刪除重復的單詞的兩種方法,一種是使用for循環,一種是使用Linq,而后者更簡單,代碼更少。

1、使用for從字符串中刪除重復的單詞

C#代碼

string SetenceString = "red white black white green yellow red red black white";
string[] data = SetenceString.Split(' ');
StringBuilder sb = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
  string temp = " ";
  if (i == 0)
  {
    temp = data[i].ToString();
    sb = sb.Append(temp + " ");
  }
  else
  {
    for (int j = 1; j < data.Length; j++)
    {
      string temp2 = data[j].ToString();
      string strnew = sb.ToString();
      string[] Kdata = strnew.Split(' ');
      bool isnoduplicate = false;
      for (int k = 0; k < Kdata.Length; k++)
      {
        string temp3 = Kdata[k].ToString();
        if (temp3 != "")
        {
          if (temp2 == temp3)
          {
            isnoduplicate = false;
            break;
          }
          else
          {
            isnoduplicate = true;
          }
        }
      }
      if (isnoduplicate)
      {
        sb = sb.Append(temp2 + " ");
      }
    }
  }
}
Console.WriteLine(sb);
Console.ReadKey();

輸出

red white black green yellow 

2、使用Linq從字符串中刪除重復的單詞

確保你的 .NET 版本4.0以上。

程序先添加System.Linq命名空間。

using System.Linq;

C#代碼

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);
}

輸出

One Two Three

System.Linq 命名空間

提供支持某些查詢的類和接口,這些查詢使用語言集成查詢 (LINQ)。

命名空間 System.Linq 位于 System.Core.dllSystem.Core 程序集中。

Enumerable 包含 LINQ 標準查詢運算符,這些運算符對實現 IEnumerable<T>的對象進行操作。

Queryable 包含 LINQ 標準查詢運算符,這些運算符對實現 IQueryable<T>的對象進行操作。

C#計算字符串中的重復字數

C#代碼

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
  
namespace CountRepeatedWordCount  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            string Word;  
            Console.WriteLine("Enter the word!..");  
            Word = Console.ReadLine();   // 在運行時讀取輸入的字符串  
            var Value = Word.Split(' ');  // 分割字符串并存儲在一個變量中  
            Dictionary<string, int> RepeatedWordCount = new Dictionary<string, int>();  
            for (int i = 0; i < Value.Length; i++) //循環已經分割的字符串  
            {  
                if (RepeatedWordCount.ContainsKey(Value[i])) // 檢查單詞是否存在,更新總數  
                {  
                    int value = RepeatedWordCount[Value[i]];  
                    RepeatedWordCount[Value[i]] = value + 1;  
                }  
                else  
                {  
                    RepeatedWordCount.Add(Value[i], 1);  // 如果字符串重復就不加入字典里,這里是加進去   
                }  
            }  
            Console.WriteLine();  
            Console.WriteLine("------------------------------------");  
            Console.WriteLine("Repeated words and counts");  
            foreach (KeyValuePair<string, int> kvp in RepeatedWordCount)  
            {  
                Console.WriteLine(kvp.Key + " Counts are " + kvp.Value);  // 打印重復的單詞及其總數  
            }  
            Console.ReadKey();  
        }  
    }  

步驟 1

復制代碼并將其粘貼到 C# 控制臺應用程序。

注意
根據你的應用程序名稱,創建具有相同名稱CountRepeatedWordCount 的應用程序或更改復制的代碼。

步驟 2

保存并運行應用程序。

現在,你可以輸入字符串并查看輸出。

 

總結

本文介紹了C#從字符串中刪除重復的單詞的兩種方法,一種是使用for循環,一種是使用Linq,而后者更簡單,代碼更少。

相關文章

標簽: CSharp  asp.net  Linq  字符串  for  
x