技術(shù)頻道導(dǎo)航
HTML/CSS
.NET技術(shù)
IIS技術(shù)
PHP技術(shù)
Js/JQuery
Photoshop
Fireworks
服務(wù)器技術(shù)
操作系統(tǒng)
網(wǎng)站運(yùn)營(yíng)

贊助商

分類目錄

贊助商

最新文章

搜索

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

作者:admin    時(shí)間:2023-5-1 1:20:8    瀏覽:

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

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

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從字符串中刪除重復(fù)的單詞

確保你的 .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 命名空間

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

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

Enumerable 包含 LINQ 標(biāo)準(zhǔn)查詢運(yùn)算符,這些運(yùn)算符對(duì)實(shí)現(xiàn) IEnumerable<T>的對(duì)象進(jìn)行操作。

Queryable 包含 LINQ 標(biāo)準(zhǔn)查詢運(yùn)算符,這些運(yùn)算符對(duì)實(shí)現(xiàn) IQueryable<T>的對(duì)象進(jìn)行操作。

C#計(jì)算字符串中的重復(fù)字?jǐn)?shù)

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();   // 在運(yùn)行時(shí)讀取輸入的字符串  
            var Value = Word.Split(' ');  // 分割字符串并存儲(chǔ)在一個(gè)變量中  
            Dictionary<string, int> RepeatedWordCount = new Dictionary<string, int>();  
            for (int i = 0; i < Value.Length; i++) //循環(huán)已經(jīng)分割的字符串  
            {  
                if (RepeatedWordCount.ContainsKey(Value[i])) // 檢查單詞是否存在,更新總數(shù)  
                {  
                    int value = RepeatedWordCount[Value[i]];  
                    RepeatedWordCount[Value[i]] = value + 1;  
                }  
                else  
                {  
                    RepeatedWordCount.Add(Value[i], 1);  // 如果字符串重復(fù)就不加入字典里,這里是加進(jìn)去   
                }  
            }  
            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);  // 打印重復(fù)的單詞及其總數(shù)  
            }  
            Console.ReadKey();  
        }  
    }  

步驟 1

復(fù)制代碼并將其粘貼到 C# 控制臺(tái)應(yīng)用程序。

注意
根據(jù)你的應(yīng)用程序名稱,創(chuàng)建具有相同名稱CountRepeatedWordCount 的應(yīng)用程序或更改復(fù)制的代碼。

步驟 2

保存并運(yùn)行應(yīng)用程序。

現(xiàn)在,你可以輸入字符串并查看輸出。

 

總結(jié)

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

相關(guān)文章

標(biāo)簽: CSharp  asp.net  Linq  字符串  for  
x
  • 站長(zhǎng)推薦
/* 左側(cè)顯示文章內(nèi)容目錄 */