Сравнение двух больших текстов - Visual Basic .NET
Формулировка задачи:
Есть два больших текста, отличающиеся друг от друга несколькими словами.
Как выявить эти слова? Как сравнить эти тексты?
Решение задачи: «Сравнение двух больших текстов»
textual
Листинг программы
Imports System.IO Public Class Form1 Dim i As Long Dim j As Long Dim tx1 As String Dim tx2 As String Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click i = 0 j = 0 ListBox1.Items.Clear() ListBox2.Items.Clear() Dim drt As Boolean Dim Text1 As String = RichTextBox1.Text ' "apple pear banana " Dim TestArray1() As String = Split(Text1) ' TestArray1 holds {"apple", "", "", "", "pear", "banana", "", ""} Dim LastNonEmpty1 As Integer = -1 For i As Integer = 0 To TestArray1.Length - 1 If TestArray1(i) <> "" Then LastNonEmpty1 += 1 TestArray1(LastNonEmpty1) = TestArray1(i) ListBox1.Items.Add(TestArray1(LastNonEmpty1)) End If Next ReDim Preserve TestArray1(LastNonEmpty1) ' TestArray1 now holds {"apple", "pear", "banana"} '/ Dim Text2 As String = RichTextBox2.Text ' "apple pear banana " Dim TestArray2() As String = Split(Text2) ' TestArray2 holds {"apple", "", "", "", "pear", "banana", "", ""} Dim LastNonEmpty2 As Integer = -1 For i As Integer = 0 To TestArray2.Length - 1 If TestArray2(i) <> "" Then LastNonEmpty2 += 1 TestArray2(LastNonEmpty2) = TestArray2(i) ListBox2.Items.Add(TestArray2(LastNonEmpty2)) End If Next ReDim Preserve TestArray2(LastNonEmpty2) '/ Построчное сравнение слов Dim kls As Long If ListBox1.Items.Count > ListBox2.Items.Count Then kls = ListBox2.Items.Count drt = True Else kls = ListBox1.Items.Count drt = False End If For i = 0 To kls - 1 If ListBox1.Items(i) <> ListBox2.Items(i) Then ' MsgBox(ListBox1.Items(i) & " " & ListBox2.Items(i)) tx1 = ListBox1.Items(i) ' Слово за которым пропущенное слово tx2 = ListBox2.Items(i) ' Слово, отсутствующее в ListBox1 If drt = True Then FindMyString(tx1) j = ListBox1.FindString(tx1, i) ListBox1.SelectedIndex = j RichTextBox1.Select(RichTextBox1.Text.IndexOf(ListBox1.Items(i)), Len(ListBox1.Items(i))) RichTextBox1.SelectionColor = Color.Red RichTextBox1.HideSelection = False RichTextBox1.SelectionStart = RichTextBox1.Text.Length RichTextBox1.ScrollToCaret() Exit For Else ' Если слово отсутствует в 1-м ричбоксе FindMyString(tx2) j = ListBox2.FindString(tx2, i) ListBox2.SelectedIndex = j ' MsgBox(ListBox2.Items(i)) RichTextBox2.Select(RichTextBox2.Text.IndexOf(ListBox2.Items(i)), Len(ListBox2.Items(i))) RichTextBox2.SelectionColor = Color.Red RichTextBox2.HideSelection = False RichTextBox2.SelectionStart = RichTextBox2.Text.Length RichTextBox2.ScrollToCaret() Exit For End If '// End If Next '/ '// End Sub '/ Private Sub FindMyString(ByVal searchString As String) ' Ensure we have a proper string to search for. If searchString <> String.Empty Then ' Find the item in the list and store the index to the item. Dim index As Integer = listBox1.FindString(searchString) ' Determine if a valid index is returned. Select the item if it is valid. If index <> -1 Then listBox1.SetSelected(index, True) Else ' MessageBox.Show("The search string did not match any items in the ListBox") End If End If End Sub '// End Class