Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. HUH ??

HUH ??

Scheduled Pinned Locked Moved C#
question
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    IceWater42
    wrote on last edited by
    #1

    Hi ... I only want to remove the FIRST "ma" but it appears that i must use a string method on a copy of a StringBuilder variable in order to execute the Remove method of StringBuilder. string pat = "ma"; string dictWord = "Wrestlemamania"; StringBuilder w = new StringBuilder(); w.Length = 0 w.Append(dictWord); int result = dictWord.IndexOf(pat); // Why must i look at a STRING variable dictWord = dictWord.Remove(result, 2); // in order to do THIS w.Remove(result, 2); // using a StringBuilder variable? I can't find the equivalent "IndexOf" in the StringBuilder methods. How would this be done using StringBuilder? What am i missing? Thanks.

    D J 2 Replies Last reply
    0
    • I IceWater42

      Hi ... I only want to remove the FIRST "ma" but it appears that i must use a string method on a copy of a StringBuilder variable in order to execute the Remove method of StringBuilder. string pat = "ma"; string dictWord = "Wrestlemamania"; StringBuilder w = new StringBuilder(); w.Length = 0 w.Append(dictWord); int result = dictWord.IndexOf(pat); // Why must i look at a STRING variable dictWord = dictWord.Remove(result, 2); // in order to do THIS w.Remove(result, 2); // using a StringBuilder variable? I can't find the equivalent "IndexOf" in the StringBuilder methods. How would this be done using StringBuilder? What am i missing? Thanks.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You're doing this the hard way. All you need to do is find the position of the first occurance of "ma" in the string, then call the Remove method on that string, supplying the starting position and how many characters to remove, which will return a new string.

      string pat = "ma";
      string dictWord = "Wrestlemamania";
      

      int pos = dictWord.IndexOf( pat );
      dictWord = dictWord.Remove( pos, pat.Length );

      RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      I 1 Reply Last reply
      0
      • I IceWater42

        Hi ... I only want to remove the FIRST "ma" but it appears that i must use a string method on a copy of a StringBuilder variable in order to execute the Remove method of StringBuilder. string pat = "ma"; string dictWord = "Wrestlemamania"; StringBuilder w = new StringBuilder(); w.Length = 0 w.Append(dictWord); int result = dictWord.IndexOf(pat); // Why must i look at a STRING variable dictWord = dictWord.Remove(result, 2); // in order to do THIS w.Remove(result, 2); // using a StringBuilder variable? I can't find the equivalent "IndexOf" in the StringBuilder methods. How would this be done using StringBuilder? What am i missing? Thanks.

        J Offline
        J Offline
        Judah Gabriel Himango
        wrote on last edited by
        #3

        StringBuilder is for building strings, not examining them. Instead of string manipulation like this, you might want to look at System.Text.RegularExpressions

        Tech, life, family, faith: Give me a visit. I'm currently blogging about: Bought a House! Judah Himango

        1 Reply Last reply
        0
        • D Dave Kreskowiak

          You're doing this the hard way. All you need to do is find the position of the first occurance of "ma" in the string, then call the Remove method on that string, supplying the starting position and how many characters to remove, which will return a new string.

          string pat = "ma";
          string dictWord = "Wrestlemamania";
          

          int pos = dictWord.IndexOf( pat );
          dictWord = dictWord.Remove( pos, pat.Length );

          RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

          I Offline
          I Offline
          IceWater42
          wrote on last edited by
          #4

          Yes ... you are correct, however, the question was not whether it could be done with STRINGSs, but rather how to do it with STRINGBUILDER. In fact, i am currently using STRINGs ... precisely as you describe. The problem occurs when you have hundreds of thousands of these things to do ... performance becomes important ... and i wondered if i could speed things up if i could avoid CONSTRUCTING the resulting millions of intermediate strings needed in the complete solution. IMHO, the need to create a STRING negates most of the value associated with using STRINGBUILDER ... in fact, it makes matters worse. I only showed part of the problem ... the part that was perplexing: the lack of an "IndexOf" method for STRINGBUILDER.

          D 1 Reply Last reply
          0
          • I IceWater42

            Yes ... you are correct, however, the question was not whether it could be done with STRINGSs, but rather how to do it with STRINGBUILDER. In fact, i am currently using STRINGs ... precisely as you describe. The problem occurs when you have hundreds of thousands of these things to do ... performance becomes important ... and i wondered if i could speed things up if i could avoid CONSTRUCTING the resulting millions of intermediate strings needed in the complete solution. IMHO, the need to create a STRING negates most of the value associated with using STRINGBUILDER ... in fact, it makes matters worse. I only showed part of the problem ... the part that was perplexing: the lack of an "IndexOf" method for STRINGBUILDER.

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            IceWater42 wrote:

            the part that was perplexing: the lack of an "IndexOf" method for STRINGBUILDER.

            There is no "IndexOf" because a StringBuilder treats the string as an array of characters. There is no way to tell what the first occurance of a string is because you can't compare a string to an array of characters. It could be done with custom coding to comparision, but the performance hit in this code would more than likely be worse than rebuilding the string using string objects. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups