.net how to compare a stringbuilder to a string

∥☆過路亽.° 提交于 2019-12-12 04:37:06

问题


In vb.net (or C#) I can't figure out how to compare a stringbuilder to a string. I have searched quite a bit and can't find the answer. I had to write my own routine. Isn't there a better way?

This doesn't work:

Dim s As String = "abc"
Dim sb As New StringBuilder("abc")
If sb.Equals(s) Then
  Console.WriteLine(sb.ToString() + " DOES equal " + s)
Else
  Console.WriteLine(sb.ToString() + " does NOT equal " + s)
End If

Results of that code is: abc does NOT equal abc

Isn't there some way of comparing a stringbuilder to a string without writing my own routine? It's probably something obvious that I'm missing since I can't find this question anywhere.


回答1:


The simplest way is to get the content of the StringBuilder as a string:

If sb.ToString() = s Then ...

If you want to avoid creating that string (perhaps for memory usage concerns), I am afraid that you have to write your own routine to compare them. Basically something like:

Public Shared Function SbEquals(sb As StringBuilder, s As String) As Boolean
  If sb.Length <> s.Length Then Return False
  For i As Integer = 0 to s.Length - 1
    If sb(i) <> s(i) Return False
  Next
  Return True
End Function



回答2:


Use

if sb.ToString() = s Then

Currently you are comparing StringBuilder instance to string and not their values. To get the value of StringBuilder object, you have to call ToString and then you can compare it with a string.




回答3:


A StringBuilder has a ToString() method.

in C#:

StringBuilder sb = new StringBuilder("test");
if (sb.ToString() == "test")
{
    // code here
}



回答4:


Use proper string comparison instead of a reference compare.

If String.Compare(sb.ToString(), s, ignoreCase) = 0 Then

You are comparing the memory locations of the string builder and the string not the contents.




回答5:


A simple c# solution (middle road performance)

public static bool EqualStrings(StringBuilder stringBuilder, ref string text)
{
    return stringBuilder.Length == text.Length && stringBuilder.ToString() == text;
}



回答6:


Here's my code for the tostring() test versus my own routine:

Sub speedTest1()

Dim time As New System.Diagnostics.Stopwatch
time.Start()
Dim i As Integer
Dim sb As New StringBuilder("abc")
Dim s As String = "abc"
For i = 0 To 10000000
  equalsString(sb, s)
Next
time.Stop()
Console.WriteLine(CStr(time.ElapsedMilliseconds) + " MS for my own routine")

time.Start()
For i = 0 To 10000000
  equalsString2(sb, s)
Next
time.Stop()
Console.WriteLine(CStr(time.ElapsedMilliseconds) + " MS for tostring()")

End Sub

Function equalsString(pSB As StringBuilder, pStr As String) As Boolean

If pSB.Length <> pStr.Length Then Return False
For i As Integer = 0 To pStr.Length - 1
  If pSB(i) <> pStr(i) Then
    Return False
  End If
Next
Return True

End Function

Function equalsString2(pSb As StringBuilder, pStr As String) As Boolean

Return (pSb.ToString() = pStr)

End Function

And the result is: 527 MS for my own routine 1045 MS for tostring()

I can't get the stupid ctrl-k to work in here, but that's how I got my results



来源:https://stackoverflow.com/questions/25507290/net-how-to-compare-a-stringbuilder-to-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!