using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace TEXTBOX
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
richTextBox1.Text = "";
} private void button1_Click(object sender, EventArgs e)
{
Stream myStream;
StreamReader myReader;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "E:\\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
//openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
string sLine;
myReader = File.OpenText(openFileDialog1.FileName);
while ((sLine = myReader.ReadLine()) != null)
{
richTextBox1.Text += sLine;
} }
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
} } private void button2_Click(object sender, EventArgs e)
{
string W = richTextBox1.Text;
//定义一个字符数组
char[] c = { ' ', ',', '.', '?', '!', ':', ';', '\'', '\"' }; //分隔字符串后产生的字符串数组
string[] S = W.Split(c);
//建立哈希表
Hashtable ha = new Hashtable();
for (int i = 0; i < S.Length; i++)
{
//判断文本是否进入
if (ha.ContainsKey(S[i]))
{
ha[S[i]] = (int)ha[S[i]] + 1;
}
else
{
ha.Add(S[i], 1);
}
}
//遍历哈希表
foreach (DictionaryEntry de in ha)
{
//输出
Console.WriteLine(de.Key + ":" + de.Value);
//追加文本
richTextBox2.AppendText(de.Key + ":" + de.Value + "\n"); }
int Sum = 0;
for (int i = 0; i < S.Length; i++)
{
textBox1.Text = (i + 1).ToString(); } }
}
}
如果文章:I. am, a Boy.
统计结果是:
Boy:1
a:1
am:1
:3
I:1
单词出现次数 7次空格也被统计出来了
哪里的问题 请帮忙修正谢谢了
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace TEXTBOX
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
richTextBox1.Text = "";
} private void button1_Click(object sender, EventArgs e)
{
Stream myStream;
StreamReader myReader;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "E:\\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
//openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
string sLine;
myReader = File.OpenText(openFileDialog1.FileName);
while ((sLine = myReader.ReadLine()) != null)
{
richTextBox1.Text += sLine;
} }
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
} } private void button2_Click(object sender, EventArgs e)
{
string W = richTextBox1.Text;
//定义一个字符数组
char[] c = { ' ', ',', '.', '?', '!', ':', ';', '\'', '\"' }; //分隔字符串后产生的字符串数组
string[] S = W.Split(c);
//建立哈希表
Hashtable ha = new Hashtable();
for (int i = 0; i < S.Length; i++)
{
//判断文本是否进入
if (ha.ContainsKey(S[i]))
{
ha[S[i]] = (int)ha[S[i]] + 1;
}
else
{
ha.Add(S[i], 1);
}
}
//遍历哈希表
foreach (DictionaryEntry de in ha)
{
//输出
Console.WriteLine(de.Key + ":" + de.Value);
//追加文本
richTextBox2.AppendText(de.Key + ":" + de.Value + "\n"); }
int Sum = 0;
for (int i = 0; i < S.Length; i++)
{
textBox1.Text = (i + 1).ToString(); } }
}
}
如果文章:I. am, a Boy.
统计结果是:
Boy:1
a:1
am:1
:3
I:1
单词出现次数 7次空格也被统计出来了
哪里的问题 请帮忙修正谢谢了
解决方案 »
- C# window 实现 xml文件 显示在treeView ,,后续还有其他操作....速度抢分...
- 在WEB下有 没有类型OpenFileDialog类,可能打开文件。
- 关于短信中间件Xml读取过来的问题??高手赐教!~~~
- 如何在static属性获得本类型的FullName
- 【求助】分布式系统如何编写设备服务
- 如何通过一个对象的字符串描述返回该对象的实例?
- 提供framework2.0下载!
- 如何控制 VS2005 下 MenuStrip、ToolStrip 控件颜色不随着 Windows XP 的主题颜色变化?
- 各位大虾:有没有代码可以创建sql server 客户端网络实用工具中的别名,还是一定要去手工创建,等待......
- c# winform怎么实现涂鸦?
- 关于程序进程
- updata语句问题,100分献上,大侠请进,实在看不出错在那里,在线等
//改为
string[] S = W.Split(c, StringSplitOptions.RemoveEmptyEntries);
我记得原来java这样做过