简体   繁体   English

如何在vb中对字符串排序

[英]how to sort strings in vb

I am stuck with this problem. 我陷入这个问题。 I am using Visual Studio Express 2013 (is that VB.NET?) and this is the task I would like to perform: I have a .txt file formatted like this (many more lines): 我正在使用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

They are HEX values, I need to find repeating patterns. 它们是十六进制值,我需要找到重复的模式。 The pattern can be 2, 3, 4 or 5 bytes long, but for the moment it's ok to do it only with fixed size (ie only 2 bytes pattern). 模式可以是2、3、4或5个字节长,但目前只可以使用固定大小(即,仅2个字节的模式)即可。 I want to populate a SortedDictionary with (Hex pattern, Repetition) I tried SortedDictionary and Dictionary, same issue. 我想用(十六进制模式,重复)填充SortedDictionary,我尝试了SortedDictionary和Dictionary,同样的问题。 I have tested it for 10 lines or so step-by-step F11 debug and it works ok. 我已经对其进行了10行左右的F11调试测试,并且工作正常。 However if I run the application with a 120 lines text file (which is small compared to what I want to do), it just hangs. 但是,如果我使用120行文本文件运行该应用程序(与我要执行的操作相比,该文件很小),它将挂起。 The maximum number of elements in the dictionary would be 256*256, is it too much? 字典中元素的最大数量为256 * 256,是否太多了? What then with 3,4 or 5 bytes? 那么3,4或5个字节又如何呢? It's not only a matter of time, the debugger raises an exception because the process doesn't end in 60 seconds. 这不仅是时间问题,调试器还会引发异常,因为该过程不会在60秒内结束。 Is there a more clever way of doing what I want to do? 有没有更聪明的方式来做我想做的事?

    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

Thanks in advance to everyone who helps! 在此先感谢所有提供帮助的人!

If you intend to find repeated single bytes (hex-digit pairs) such as 00 00 00 00 then something like this would work: 如果您打算查找重复的单个字节(十六进制数字对),例如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

results 结果

'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

That's only for a single line (your 2nd line of your sample input), but you can easily expand that to work over each of the lines from the file. 这仅适用于单行(示例输入的第二行),但是您可以轻松地对其进行扩展以处理文件中的每一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM