从字符串中删除重复的字符并找到最短的字符

时间:2019-05-12 09:56:08

标签: vb.net file line

我不知道如何从结果或最短行本身或其编号中进行选择

(是的,这种古老的运营商需要解决方案)

Imports System.IO
Public Class Form1
    Sub readputh(ByRef s As String)
        s = ""
        OpenFileDialog1.Filter = "Textfiles (*.txt)|*.txt"
        OpenFileDialog1.ShowDialog()
        Do While s = ""
            s = OpenFileDialog1.FileName
        Loop
    End Sub
    Sub writeputh(ByRef s As String)
        s = ""
        SaveFileDialog1.Filter = "Textfiles (*.txt)|*.txt"
        SaveFileDialog1.ShowDialog()
        Do While s = ""
            s = SaveFileDialog1.FileName
        Loop
    End Sub

    Sub ch(ByVal Str As String, ByRef Res As String)
        Dim a As Char
        Res = Mid(Str, 1, 1)
        For i = 2 To Len(Str)
            a = CChar(Mid(Str, i, 1))
            If InStr(Res, a) = 0 Then
                Res = Res + a
            End If
        Next
    End Sub

    Sub resh(ByVal filename1 As String, ByVal filename2 As String, ByRef lb1 As ListBox, ByRef lb2 As ListBox)
        Dim rf As StreamReader
        Dim wf As StreamWriter
        Dim s1, s2, s3 As String
        s2 = ""
        s3 = ""
        Try
            rf = New StreamReader(filename1)
            wf = New StreamWriter(filename2, True)
            Do While Not rf.EndOfStream()
                s1 = rf.ReadLine()
                lb1.Items.Add(s1)
                ch(s1, s2)
                wf.WriteLine(s2)
                lb2.Items.Add(s2)
            Loop
            wf.Close()
            rf.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim filename1, filename2 As String
        readputh(filename1)
        writeputh(filename2)
        resh(filename1, filename2, ListBox1, ListBox2)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End
    End Sub
End Class

输入文件: youtubeyoutubeyotube 狗狗 geeksforgeeks

输出文件: Youtbe 狗 geksfor

但是我希望输出文件只有“ dog”

1 个答案:

答案 0 :(得分:0)

我只是无法处理旧代码。一种基础功能?没有!您可以将其翻译回想要的内容,但现在输出为dog。

Private Function GetOpenPath() As String
    OpenFileDialog1.Filter = "Textfiles (*.txt)|*.txt"
    If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
        Return OpenFileDialog1.FileName
    Else
        Return Nothing
    End If
End Function

Private Function GetSavePath() As String
    SaveFileDialog1.Filter = "Textfiles (*.txt)|*.txt"
    If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
        Return SaveFileDialog1.FileName
    Else
        Return Nothing
    End If
End Function

Private Function ch(ByVal Str As String) As String
    Dim a As Char
    Dim Res = Str.Substring(0, 1)
    For i = 1 To Str.Length - 1
        a = CChar(Str.Substring(i, 1))
        If Res.IndexOf(a) = -1 Then
            Res &= a
        End If
    Next
    Return Res
End Function

Sub resh(ByVal filename1 As String, ByVal filename2 As String)
    Dim lines = File.ReadAllLines(filename1)         
    Dim NewLines As New List(Of String)
    For Each line In lines
        Try
            ListBox1.Items.Add(line)
            Dim s2 = ch(line)
            NewLines.Add(s2)
            ListBox2.Items.Add(s2)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    Next
    Dim Shortest = NewLines.OrderByDescending(Function(s) s.Length).Last()
    File.WriteAllText(filename2, Shortest)
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim OpenPath = GetOpenPath()
    Dim SavePath = GetSavePath()
    If String.IsNullOrEmpty(OpenPath) OrElse String.IsNullOrEmpty(SavePath) Then
        MessageBox.Show("You must provide a file. Try again.")
        Return
    End If
    resh(OpenPath, SavePath)
End Sub
相关问题