How to search for a keyword in a StringBuilder?

L

Laser Lu

Sometimes, I need to do some time-consuming operations based on whether a
specific keyword was contained in a lengthy string. Then, for a better
performance, I wrapped that lengthy string into a StringBuilder.
But after doing that, it seems there's no proper methods defined in
StringBuilder for me to search for a specific keyword. How to resolve this
problem? Does this mean I can not use StringBuilder for keyword search?
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Laser said:
Sometimes, I need to do some time-consuming operations based on whether a
specific keyword was contained in a lengthy string. Then, for a better
performance, I wrapped that lengthy string into a StringBuilder.
But after doing that, it seems there's no proper methods defined in
StringBuilder for me to search for a specific keyword. How to resolve this
problem? Does this mean I can not use StringBuilder for keyword search?

Yes, a StringBuilder has no methods for searching in the string.

Are you modifying the string, so that's the reason that you put it in a
StringBuilder?
 
L

Laser Lu

Yes, I need to find the keyword and replace it with another string in the
StringBuilder.
 
L

Laser Lu

Göran Andersson said:
Yes, a StringBuilder has no methods for searching in the string.

Are you modifying the string, so that's the reason that you put it in a
StringBuilder?

Yes, modification on that lengthy string may be required.
First, I need to find out whether the keyword was in the string. If it was,
then a time-consuming operation will be taken and return the keyword
substitution. Finally, replace all the keywords in the string with that
substitution. That's the whole process. I just wonder how to do it in a
graceful way with a better performance if I can not do searchs in
StringBuilder?
 
S

Stephany Young

First of all, whether you use a StringBuilder object or not will have no
effect on the 'time-consuming operation'.

The StringBuilder object, as its name suggests, is used for building strings
and manipulating. It's properties and methods reflect this. It is not a
StringSearcher object.

You already have the original string (because you have loaded it into the
StringBuilder object) so simply use that string for the search operations:

Private Function Manipulate String(ByVal myString As String, ByVal
myKeyword As String)

If myString.Contains(myKeyword) Then
Dim _sb As New StringBuilder(myString)
_sb.Replace(myKeyword, TimeConsumingOperation(myKeyword))
Return _sb.ToString
End If

Return myString

End Function

However, taking the overhead of loading the Stringbuilder object and then
'extracting' the resultant string into account, I would suggest that NOT
using a StringBuilder object would probably be faster and more efficient,
thus:

Private Function Manipulate String(ByVal myString As String, ByVal
myKeyword As String)

If myString.Contains(myKeyword) Then Return myString.Replace(myKeyword,
TimeConsumingOperation(myKeyword))

Return myString

End Function
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Laser said:
Yes, modification on that lengthy string may be required.
First, I need to find out whether the keyword was in the string. If it was,
then a time-consuming operation will be taken and return the keyword
substitution. Finally, replace all the keywords in the string with that
substitution. That's the whole process. I just wonder how to do it in a
graceful way with a better performance if I can not do searchs in
StringBuilder?

If you use a Regex.Replace with a pattern that matches any of the
keywords you want to replace, and use a delegate to return the
substitution for each keyword, you can do all replacements in one go.
 
J

Jeffrey Palermo

Sometimes, I need to do some time-consuming operations based on whether a
specific keyword was contained in a lengthy string. Then, for a better
performance, I wrapped that lengthy string into a StringBuilder.
But after doing that, it seems there's no proper methods defined in
StringBuilder for me to search for a specific keyword. How to resolve this
problem? Does this mean I can not use StringBuilder for keyword search?

It sounds to me that you have some business logic taking place that
needs to affect the formatted text that is your end result (contained
in the StringBuilder). If I'm right about this, then it's premature
to be formatting the string in the StringBuilder or building up the
string. You might need to augment your domain model with the concepts
you are using for the output. Then, with all the information in
object form, you can easily modify distinct parts without having to
worry about error-prone string operations. When the process is
complete, you can easily convert your objects into a properly-
formatted string and output the result.

Holding information in large strings is very cumbersome to manage, so
keep information in your rich domain model as long as possible, and
when all decisions have been made, then format it appropriately in a
string and display.

Best regards,
Jeffrey Palermo
 
L

Laser Lu

Aha, thank you very much! Using Regex.Replace() and MatchEvaluator(which is
a delegate only be invoked after match is found) can indeed solve this
problem with a higher performance.:)
 
V

vMike

Laser Lu said:
Sometimes, I need to do some time-consuming operations based on whether a
specific keyword was contained in a lengthy string. Then, for a better
performance, I wrapped that lengthy string into a StringBuilder.
But after doing that, it seems there's no proper methods defined in
StringBuilder for me to search for a specific keyword. How to resolve this
problem? Does this mean I can not use StringBuilder for keyword search?
you can use the stringbuilder.replace method and it will replace all
occurrences if that is what you are looking for. My understanding is
stringbuilder is more efficient from a memory point of view than using
string as I believed that every string operation requires the use of new
memory and stringbuilding operation does not.
See this discussion
http://groups.google.com/group/micr...r+versus+string&rnum=1&hl=en#eadaa26e3593ee9a


Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top