Results 1 to 8 of 8

Thread: Count replaces in Stringbuilder

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2004
    Posts
    541

    Count replaces in Stringbuilder

    Hello,

    Just curious if this can be done. Is there a way to count the number of replaces or finds in a stringbuilder?

    Note: I want to avoid having to loop through the file to count them prior to performing the replace. This'll probably be the route I go if it can't be done in Stringbuilder. I'm using VB 2005.

    Thanks in advance,

    Strick

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Count replaces in Stringbuilder

    vb Code:
    1. Dim sb As New System.Text.StringBuilder("Hi I am Some data I am!")
    2. Dim replaceArg As String = "I"
    3. Dim replacements As Integer = (sb.Length - sb.Replace(replaceArg, "").Length) / replaceArg.Length

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Count replaces in Stringbuilder

    Bill that looks like it would only work if you were replacing single characters. What about if you were to replace a 4 letter word with "". Your result would be 4, when the actual number of replacements is actually 1.

    I guess not enough specs were provided by the thread starter to give an accurate answer though.

  4. #4
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Count replaces in Stringbuilder

    (sb.Length - sb.Replace(replaceArg, "").Length) = 4
    replaceArg.Length = 4

    4 /4 = 1

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Count replaces in Stringbuilder

    Quote Originally Posted by wild_bill
    vb Code:
    1. Dim sb As New System.Text.StringBuilder("Hi I am Some data I am!")
    2. Dim replaceArg As String = "I"
    3. Dim replacements As Integer = (sb.Length - sb.Replace(replaceArg, "").Length) / replaceArg.Length
    That's all very well if you're removing substrings by replacing them with an empty string. What if you're replacing substrings with another substring of equal length? The original and the final will both have the same length so your result would be zero regardless.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Count replaces in Stringbuilder

    True, but I thought he just wanted a count. Another option would be to write a simple regex.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2004
    Posts
    541

    Re: Count replaces in Stringbuilder

    Hi, I'm looking to count the number of replace(s) done. This can be a word or single character. So,

    Dim sb As New System.Text.StringBuilder("This is test1. This is test2")

    So if i replace "This" with "That" the count would be 2 and both "this" would get replaced with "that"


    Thanks,

    Strick

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Count replaces in Stringbuilder

    The StringBuilder doesn't provide that functionality, plain and simple. If it did then it would be documented in the Help for the Replace method. It is up to you to count the number of substrings that will be replaced before you replace them. Wild_bill's last suggestion of using a Regex would be the best way of doing that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width