HUH ??
-
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.
-
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.
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
-
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.
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
-
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
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.
-
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.
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