簡體   English   中英

如何在vb中對字符串排序

[英]how to sort strings in vb

我陷入這個問題。 我正在使用Visual Studio Express 2013(是VB.NET嗎?),這是我要執行的任務:我有一個.txt文件,其格式如下(多行):

b2 a9 9c b4 d2 d3 52 02 da d2 e2 a2 c2 34 b2 a4 25 1c cb 52
00 00 00 00 00 00 00 00 00 00 04 c6 a2 10 a2 aa 5a 96 12 35 00 00 00 00 00 00 00 00 00 00 04 ae a5 9b 53 6c 15 56 56 d2 a1 54 55 b4 a6 ba a8 aa a6 b9 a8 44 94 69 5e d1 17 6a 56 9a 0b a7 29 49
69 d2 14 11 a1 78 41 d0 a4 54 41 51 1c 94 c1 24 a8 2a 71 14 50 14 04 b5 45 31 00 00 00 00 00 00 00 00 00 00

它們是十六進制值,我需要找到重復的模式。 模式可以是2、3、4或5個字節長,但目前只可以使用固定大小(即,僅2個字節的模式)即可。 我想用(十六進制模式,重復)填充SortedDictionary,我嘗試了SortedDictionary和Dictionary,同樣的問題。 我已經對其進行了10行左右的F11調試測試,並且工作正常。 但是,如果我使用120行文本文件運行該應用程序(與我要執行的操作相比,該文件很小),它將掛起。 字典中元素的最大數量為256 * 256,是否太多了? 那么3,4或5個字節又如何呢? 這不僅是時間問題,調試器還會引發異常,因為該過程不會在60秒內結束。 有沒有更聰明的方式來做我想做的事?

    Dim BytesList As New SortedDictionary(Of String, Integer)
    Dim line As String = ""
    Dim CurrentString As String
    Dim ByteLen As Byte

    For ByteLen = 2 To 2 'to do: ideally repeat for ByteLen = 2,3,4,5
        Using sr As StreamReader = New StreamReader(path & LOGFILENAME,False)
            Do
                line = line & sr.ReadLine()
                line = line.Replace(Chr(32), "") 'remove unwanted chars from line
                line = line.Replace(Chr(10), "")
                line = line.Replace(Chr(13), "")
                While (line.Length > ByteLen * 2)
                    CurrentString = line.Substring(0, ByteLen * 2)
                    line = line.Substring(2, line.Length - 2)
                    Try
                        BytesList.Add(CurrentString, 1)     'insert found address
                    Catch
                        BytesList(CurrentString) = BytesList(CurrentString) + 1 ' if string is already present, increment value
                    End Try
                End While
            Loop Until line Is Nothing
        End Using
    Next ByteLen
End Sub

在此先感謝所有提供幫助的人!

如果您打算查找重復的單個字節(十六進制數字對),例如00 00 00 00那么類似的方法將起作用:

Dim pattern As String = "(?<bh>\d\d )(\k<bh>)+"
Dim rx As New Regex(pattern)
Dim match = rx.Match("00 00 00 00 00 00 00 00 00 00 04 c6 a2 10 a2 aa 5a 96 12 35 00 00 00 00 00 00 00 00 00 00 04 ae a5 9b 53 6c 15 56 56 d2 a1 54 55 b4 a6 ba a8 aa a6 b9 a8 44 94 69 5e d1 17 6a 56 9a 0b a7 29 49 ")
While match.Success
    Debug.WriteLine("'{0}' found at position {1}", match.Value, match.Index)
    match = match.NextMatch()
End While

結果

'00 00 00 00 00 00 00 00 00 00 ' found at 0
'00 00 00 00 00 00 00 00 00 00 ' found at 60
'56 56 ' found at 111

這僅適用於單行(示例輸入的第二行),但是您可以輕松地對其進行擴展以處理文件中的每一行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM