Search code examples
vb.netfileline

Remove duplicate characters from strings and find the shortest


I can not figure out how to select from the result or the shortest line itself or its number

(Yes, the solution is needed in such ancient operators)

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

Input file: youtubeyoutubeyotube dogdogdogdog geeksforgeeks

Output file: youtbe dog geksfor

But I expect Output file of only "dog"


Solution

  • I just couldn't handle the old code. One based functions? No! You can translate it back it you want but output is now 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