消除相同的字符串

时间:2018-03-29 14:06:14

标签: vb.net

我输入文件all_words.txt

在文件中,我有一些单词的列表,例如:

JIM IS JANITOR
BALL IS FOR BASKETBALL
JIM IS LAWYER
CAIRO IS MAIN CAPITAL OF EGYPT

等...

我只需找到一个字符串并删除其他字符串,例如JIM (tab) IS JANITOR,我可以使用制表符分割,但我找到所有字符串。我尝试使用字典,但我认为这不是一个好的解决方案。

你有更好的解决方案吗?

正确的输出是:

JIM IS JANITOR
BALL IS FOR BASKETBALL
CAIRO IS MAIN CAPITAL OF EGYPT

我的代码:

Dim all_words As String() = File.ReadAllLines("all_words.txt")
Dim allWordsDict As Dictionary(Of String, String) = New Dictionary(Of String, String)
For Each awd As String In all_words
    Try
        allWordsDict.Add(awd.ToUpper(), awd.Split(vbTab).FirstOrDefault().ToUpperInvariant())
    Catch ex As Exception
    End Try
    Console.WriteLine(awd)
    Console.ReadLine()
Next

2 个答案:

答案 0 :(得分:0)

您可以使用LINQ“Group By”子句按第一个单词对行进行分组,然后取每个分组中的第一个项目:

Dim result = (
    From line In File.ReadLines("all_words.txt")
    Group By line.Split(" "c).First() Into g = Group ' assuming spaces for word separator
    Select g.First()
)

For Each line In result
    Console.WriteLine(line)
Next

结果:

JIM IS JANITOR
BALL IS FOR BASKETBALL
CAIRO IS MAIN CAPITAL OF EGYPT

答案 1 :(得分:0)

当您添加到字典时,您没有使用正确的密钥

Imports System
Imports Microsoft.VisualBasic
Imports System.Collections.Generic

Public Module Module1

    Public Sub Main()
        dim s as string = "JIM" + Chr(9) + "IS JANITOR" + chr(13) + 
        "BALL" + Chr(9) + "IS FOR BASKETBALL" + chr(13) + 
        "JIM" + Chr(9) + "IS LAWYER" + chr(13) + 
        "CAIRO" + Chr(9) + "IS MAIN CAPITAL OF EGYPT"

        Dim all_words As String() = s.Split(chr(13))
        Dim allWordsDict As Dictionary(Of String, String) = New Dictionary(Of String, String)
        For Each awd As String In all_words
            Try
                dim dictionaryWords as String() = awd.Split(vbtab)
                allWordsDict.Add(dictionaryWords(0), awd)
            Catch ex As Exception
            End Try
        Next

        'Looping dictionary
        for each entry in allWordsDict
            Console.WriteLine(entry.Value)
        next
    End Sub
End Module

<强>输出:

JIM    IS JANITOR
BALL    IS FOR BASKETBALL
CAIRO    IS MAIN CAPITAL OF EGYPT