繁体   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