RE:如何在visual basic .NET中找到重复的chars链 [英] RE: how to find a repeated chain of chars in visual basic NET

查看:76
本文介绍了RE:如何在visual basic .NET中找到重复的chars链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经得到解答,这是一个老帖子。但是,我已经尝试了很长时间使用许多不同的代码(这不起作用)在富文本框中找到重复的单词,然后我在这个标题下找到了解决方案编号2,这是我发现的代码适合我!



我非常感谢示例代码,但我想找到一种方法来列出RTB1中找到的每个重复单词的单个实例Listview(即Word)的第一列中的文本,然后是同一列表视图的第二列(即频率)中的文本,我还想显示RTB1中找到的每个重复单词的频率计数?



我通过在单独的RTB(2​​)中显示duplicate_words的结果并使用短语来测试这个:这是一个测试,重复几次。我得到以下(即期望的)结果:



代码:

Hi, I recognize this question has already been answered and it's an old post. But, I have been attempting this for a long time using many different codes (which didn't work) to find repeated words in a Rich Text Box, and then I found Solution number 2 under this heading, which is the code I found that works for me!

I'm very grateful for the example code, but I would like to find a way to list a single instance of each repeated word found in RTB1 text in the first column of a Listview (i.e. Word), then in the second column (i.e. Frequency) of the same listview, I would also like to show a frequency count of each repeated word found in RTB1?

I have tested this by showing the result of duplicate_words in a separate RTB(2) and using the phrase: This is a test, repeated several times. I get the following (i.e. desired) result:

Code:

RichTextBox2.Text = duplicate_names





结果:





a

test

这个



但是,当我尝试在ListView1的Word列中显示相同的结果时,我得到以下(即不需要的)结果:



代码:



Result:

is
a
test
This

However, when I attempt to show this same result in the Word column of ListView1, I get the following (i.e. undesired) result:

Code:

'For Each Value As String In duplicate_names OR
        Dim Value As String = duplicate_names



随着


With

ListView1.Items.Add(Value)





(注:我分别尝试了For each循环和Dim值行以及LV.Items.Add行)



结果:



isatestThis



(即这显示在Word专栏的第一行?)



1.如何以相同的方式将重复的单词显示为LV1中不同行(即一个在另一行下)的列表它们在RTB2示例中显示?

2.如何在第2列中的每个单词旁边显示LV1中列出的每个重复单词的计数(即频率)。

3.如何在第三列显示重复的字频率百分比?



我希望实现的结果会是这样的:



WORD FREQUENCY%

是6 25

a 6 25

test 6 25

这6 25



我非常感谢任何帮助这是因为我花了很多时间搜索互联网和测试内容,但还没有达到预期的效果!



我有什么试过:





(Note: I have separately tried both the For each loop and Dim value line together with the LV.Items.Add line)

Result:

isatestThis

(I.e. this shows in the first row of the Word column?)

1. How can I get the repeated words to show as a list on separate rows (i.e. one under the other) in LV1 in the same way they show in the RTB2 example?
2. How can I show the count for each repeated word listed in LV1 alongside each word in column 2 (i.e. Frequency).
3. How can I show the repeated word frequency percentage in a third column?

The results I would like to achieve would be something like the following:

WORD FREQUENCY %
is 6 25
a 6 25
test 6 25
This 6 25

I would very much appreciate any help on this as I have spent a lot of time searching the internet and testing stuff, but haven't managed to achieve the desired results yet!

What I have tried:

Code: <pre>RichTextBox2.Text = duplicate_names





结果:





a

test

这个







Result:

is
a
test
This


Code: <pre>'For Each Value As String In duplicate_names OR
        Dim Value As String = duplicate_names



使用


With

ListView1.Items.Add(Value)







Result:

isatestThis





所需结果(文字总数= 24):





Desired Result (Total words in text = 24):

WORD      FREQUENCY      %
is            6          25
a             6          25
test          6          25
This          6          25

推荐答案

使用字典:Dictionary< string, int>



在文字上使用string.Split来获取单词(标记[])。



迭代标记,将它们添加到字典(计数1)或更新(计数++)。



你最后得到一个单词和频率计数字典。



使用LINQ获得总和和百分比。



Use a dictionary: Dictionary<string,int>

Use string.Split on your text to get words (tokens[]).

Iterate over the tokens, adding them to the dictionary (with count 1) or updating (count++).

You wind up with a dictionary of words and frequency counts.

Use LINQ to get sums and percentages.

int count = 0;

if (myDic.ContainsKey(token)) {
   count = myDic[token];
}

myDic[token] = ++count;


这篇关于RE:如何在visual basic .NET中找到重复的chars链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆