欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門(mén)教程網(wǎng)!

C#教程

當(dāng)前位置:主頁(yè) > 軟件編程 > C#教程 >

C#判斷單詞個(gè)數(shù)方法總結(jié)

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

方法一:

判斷英文單詞個(gè)數(shù):

using System;

namespace FindWord
{
  class Program
  {
    static void Main(string[] args)
    {
      string space = " ";
      string str = "hello world" + space;
      int count = 0;
      bool start = false;
      for (int i=0;i<str.Length;i++)
      {
        if (Char .IsLetter(str[i]))
        {
          start = true;
        }
        if (!Char.IsLetter(str[i])&&start)
        {
          count++;
          start = false;
        }
        
      }
      Console.WriteLine(count);
      Console.ReadLine();
    }
  }
}

方法二:

C#統(tǒng)計(jì)英文字符串中單詞個(gè)數(shù)思路如下:

1.使用的Hashtable(高效)集合,記錄每個(gè)單詞出現(xiàn)的次數(shù)

2.采用ArrayList對(duì)Hashtable中的Keys按字母序排列

3.排序使用插入排序(穩(wěn)定)

public void StatisticsWords(string path) {
  if (!File.Exists(path))
  {
  Console.WriteLine("文件不存在!");
  return;
  }
  Hashtable ht = new Hashtable(StringComparer.OrdinalIgnoreCase);
  StreamReader sr = new StreamReader(path, System.Text.Encoding.UTF8);
  string line = sr.ReadLine();
  string[] wordArr = null;
  int num = 0;
  while (line.Length > 0)
  {
  //  MatchCollection mc = Regex.Matches(line, @"\b[a-z]+", RegexOptions.Compiled | RegexOptions.IgnoreCase);
  //foreach (Match m in mc)
  //{
  //  if (ht.ContainsKey(m.Value))
  //  {
  //    num = Convert.ToInt32(ht[m.Value]) + 1;
  //    ht[m.Value] = num;
  //  }
  //  else
  //  {
  //    ht.Add(m.Value, 1);
  //  }
  //}
  //line = sr.ReadLine();
  wordArr = line.Split(' ');
  foreach (string s in wordArr)
  {
  if (s.Length == 0)
  continue;
  //去除標(biāo)點(diǎn)
  line = Regex.Replace(line, @"[\p{P}*]", "", RegexOptions.Compiled);
  //將單詞加入哈希表
  if (ht.ContainsKey(s))
  {
  num = Convert.ToInt32(ht[s]) + 1;
  ht[s] = num;
  }
  else
  {
  ht.Add(s, 1);
  }
  }
  line = sr.ReadLine();
  }
ArrayList keysList = new ArrayList(ht.Keys);
  //對(duì)Hashtable中的Keys按字母序排列
  keysList.Sort();
  //按次數(shù)進(jìn)行插入排序【穩(wěn)定排序】,所以相同次數(shù)的單詞依舊是字母序
  string tmp = String.Empty;
  int valueTmp = 0;
  for (int i = 1; i < keysList.Count; i++)
  {
  tmp = keysList[i].ToString();
  valueTmp = (int)ht[keysList[i]];//次數(shù)
  int j = i;
  while (j > 0 && valueTmp > (int)ht[keysList[j - 1]])
  {
  keysList[j] = keysList[j - 1];
  j--;
  }
  keysList[j] = tmp;//j=0
  }
  //打印出來(lái)
  foreach (object item in keysList)
  {
  Console.WriteLine((string)item + ":" + (string)ht[item]);
  }
  }

上一篇:C#利用GDI+畫(huà)圖的基礎(chǔ)實(shí)例教程

欄    目:C#教程

下一篇:C#基于FTP協(xié)議的簡(jiǎn)易軟件自動(dòng)升級(jí)程序

本文標(biāo)題:C#判斷單詞個(gè)數(shù)方法總結(jié)

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5222.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有