|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
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? Laser Lu wrote:
> Sometimes, I need to do some time-consuming operations based on whether a Yes, a StringBuilder has no methods for searching in the string.> 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? > Are you modifying the string, so that's the reason that you put it in a StringBuilder? Yes, I need to find the keyword and replace it with another string in the
StringBuilder. Show quote "Göran Andersson" <gu***@guffa.com> wrote in message news:eUpovlpcHHA.4344@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 Göran Andersson wrote:
> Yes, modification on that lengthy string may be required.> 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? > 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? 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 Show quote "Laser Lu" <laser***@163.com> wrote in message news:O$laB%23pcHHA.3408@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? > Laser Lu wrote:
Show quote > Göran Andersson wrote: If you use a Regex.Replace with a pattern that matches any of the >> 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? > > keywords you want to replace, and use a delegate to return the substitution for each keyword, you can do all replacements in one go. 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.:) Show quote "Göran Andersson" <gu***@guffa.com> wrote in message news:OM280ZscHHA.2316@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 > On Mar 30, 1:44 am, "Laser Lu" <laser***@163.com> wrote: It sounds to me that you have some business logic taking place that> 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? 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 "Laser Lu" <laser***@163.com> wrote in message you can use the stringbuilder.replace method and it will replace all news:uHkSpbpcHHA.1244@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? 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/microsoft.public.dotnet.languages.csharp/browse_thread/thread/f12de93b39b082b2/eadaa26e3593ee9a?lnk=st&q=stringbuilder+versus+string&rnum=1&hl=en#eadaa26e3593ee9a Mike |
|||||||||||||||||||||||