473,386 Members | 1,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to search for a keyword in a StringBuilder?

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?
Mar 30 '07 #1
8 13036
Laser Lu wrote:
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?

--
Göran Andersson
_____
http://www.guffa.com
Mar 30 '07 #2
Yes, I need to find the keyword and replace it with another string in the
StringBuilder.

"Göran Andersson" <gu***@guffa.comwrote in message
news:eU**************@TK2MSFTNGP02.phx.gbl...
Laser Lu wrote:
>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?

--
Göran Andersson
_____
http://www.guffa.com

Mar 30 '07 #3
Göran Andersson wrote:
>
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?
Mar 30 '07 #4
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
"Laser Lu" <la******@163.comwrote in message
news:O$****************@TK2MSFTNGP03.phx.gbl...
Göran Andersson wrote:
>>
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?
Mar 30 '07 #5
Laser Lu wrote:
Göran Andersson wrote:
>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?

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.

--
Göran Andersson
_____
http://www.guffa.com
Mar 30 '07 #6
On Mar 30, 1:44 am, "Laser Lu" <laser...@163.comwrote:
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

Mar 30 '07 #7
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.:)

"Göran Andersson" <gu***@guffa.comwrote in message
news:OM**************@TK2MSFTNGP04.phx.gbl...
Laser Lu wrote:
>Göran Andersson wrote:
>>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?

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.

--
Göran Andersson
_____
http://www.guffa.com

Mar 31 '07 #8

"Laser Lu" <la******@163.comwrote in message
news:uH**************@TK2MSFTNGP04.phx.gbl...
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/micro...daa26e3593ee9a
Mike
Mar 31 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Phil Powell | last post by:
The table already has a fulltext index and from there I can use the MySQL fulltext search query to get results as well as the relevancy score. The problem I have is that MySQL has a default...
1
by: Matt | last post by:
I'm looking for some code that would generate a SQL statement from a single textbox in which a user could type in any number of words and quote- enclosed phrases, just like Google or any other...
3
by: Chung Leong | last post by:
Here's the rest of the tutorial I started earlier: Aside from text within a document, Indexing Service let you search on meta information stored in the files. For example, MusicArtist and...
19
by: bb nicole | last post by:
Below is my search engine for job portal which jobseeker can find the job through quick search. But it cant work... Is it mysql query got problem?? Thanx.. Interface <html> <head> <title>UMS...
8
by: Laser Lu | last post by:
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...
5
by: mforema | last post by:
Hi Everyone, I want to search records by typing in multiple keywords. I currently have a search form. It has a combo box, text box, Search command button, and a subform. The combo box lists the...
2
by: rlemusic | last post by:
Hi everybody, I’m creating a database in Access (I believe it’s 2000) to catalogue items in the archives of a small museum. I’m a total n00b as far as using Access goes, but by looking at some...
3
by: Redbeard | last post by:
Hi All this is my first time post, be gentle. I am looking at creating a keyword search that searches multiple fields in a Form and then filters records that match the keyword. The Form...
12
by: iahamed | last post by:
Hi Everyone, I got two parts of my advance search to work, I am running out of Logic to connect the third. My mind is in swing! Pleaseeeeeeeee Help me. I have 3 Fiels to search, the First two...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.