2

我正在使用此功能来反转文本,但速度有点问题。为了测试,我有 130,000 个字符的文本,大约需要 10 秒。有可能加快速度吗?这个问题与 C# 不同,因为它是一个 vb.net

Function ReverseString(ByRef strString As String) As String
        Dim NextChr, TempString As String, StringLength, Count As Integer, NewString As String = Nothing

        TempString = strString
        StringLength = Len(TempString)
        Do While Count <= StringLength
            Count = Count + 1
            NextChr = Mid(TempString, Count, 1)
            NewString = NextChr & NewString
        Loop
        ReverseString = NewString
End Function
4

5 回答 5

3

尝试这个:

 Function Reverse(ByVal value As String) As String
    ' Convert to char array.
    Dim arr() As Char = value.ToCharArray()
    ' Use Array.Reverse function.
    Array.Reverse(arr)
    ' Construct new string.
    Return New String(arr)
    End Function

来源:点网 perls

于 2013-10-17T01:52:55.253 回答
2

也许类似于http://msdn.microsoft.com/en-us/library/e462ax87(v=vs.90).aspx?在 VB 中:

Dim TestString As String = "ABCDEFG" 
' Returns "GFEDCBA". 
Dim revString As String = StrReverse(TestString)
于 2013-10-17T01:53:45.777 回答
2
Function ReverseString(ByRef strString As String) As String
    Dim charArray As Char() = strString.ToCharArray()
    Array.Reverse(charArray )
    Dim strReversed As New String(charArray )
    ReverseString = strReversed
End Function
于 2013-10-17T01:53:56.387 回答
0

我会将您的字符串转换为字符数组,然后调用Array.Reverse.

我刚刚尝试过,它运行了:0.862 秒,字符串包含 26,673,152 个字符。当然,我在一个非常快的 PC 上,但仍然如此。

于 2013-10-17T01:57:17.850 回答
-1

正如在其他答案中所说 - 最好为此使用特殊功能:StrReverse

但是,如果你想拥有自己的功能,你可以使用这个,它应该更快:

Function ReverseString(ByRef strString As String) As String
    Dim builder As New System.Text.StringBuilder(strString.Length)
    Dim index As Integer = strString.Length - 1
    While index >= 0
        builder.Append(strString.Chars(index))
        index = index - 1
    End While
    ReverseString = builder.ToString
End Function
于 2013-10-17T02:06:14.720 回答