I have a stringbuilder in VB
I like to check to see if it contains a value if so I like to do something:
If strMsg.Contains("<table>") Then
' strMsg = strMsg + "<br/><br/><br/>"
strMsg.Append("<br/><br/><br/>")
End If
I tried the above but said contains is not a member of System.Text.StringBuilder.
What can I use in place of Contains
I would check your inputs into the StringBuilder for this condition.
Dim hasTable As Boolean = False
' Check inputs, set hasTable to True if needed
If hasTable Then
strMsg.Append("<br/><br/><br/>")
End If
Contains() is not among Stringbuilder's methods. That said, you have a couple options...
1.: Test your values on the way in and maintain a boolean flag for your append <br> tags state.
2.: Perform ToString() and call Contains() from the result:
if strMsg.ToString().Contains("<table>") then '...
3.: Implement IndexOf() and/or Contains() yourself:
Module StringBuilderExtensions
<Extension()>
Public Function IndexOf(ByVal sb As StringBuilder, ByVal value As String) As Integer
For i As Integer = 0 To sb.Length - value.Length - 1
For y As Integer = 0 To value.Length - 1
If value(y) <> sb(i + y) Then
Exit For
ElseIf y = value.Length - 1 Then
Return i
End If
Next
Next
Return -1
End Function
<Extension()>
Public Function Contains(ByVal sb As StringBuilder, ByVal value As String) As Boolean
Return sb.IndexOf(value) > -1
End Function
End Module
Disclaimer: This hasn't been tested for performance... and Michael Haren's comment was directed at the ToString() option.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With