|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
My own version of replaceI try to code my own version of replace but it doesn't work. I hope somebody can help me out. It only replaces the first char!. Why I don't use the public string.replace function? Well I love implementing string functions :-). I coded another function for replacing chars in a word and this function works! Thank you for any help Use: string text = "Good Morning"; replaceStr(text,"Morning","Evening"); Console.WriteLine(text); Output: Good Eorning // Only the first char Here is the function public static void replaceStr(string word, string word2, string replace) { int i; int x = 0, y, z = 0, l = 0; char[] place = new char[word.Length]; for (i = 0; i < word.Length; i++) { place[x] = word[i]; // fills the place Array x++; } for (y = 0; y < place.Length; y++) { if (place[y] == word2[z]) / { place[y] = replace[l]; // It only replaces the first char l++; y++; z++; } } Console.WriteLine(place); }
Show quote
"implement" wrote: I think you are going to have more problems than this, but, you should not > Hi all! > > I try to code my own version of replace but it doesn't work. I hope > somebody can help me out. > It only replaces the first char!. > > Why I don't use the public string.replace function? Well I love > implementing string functions :-). > > I coded another function for replacing chars in a word and this > function works! > > Thank you for any help > > Use: > > string text = "Good Morning"; > replaceStr(text,"Morning","Evening"); > Console.WriteLine(text); > > Output: Good Eorning // Only the first char > > Here is the function > > public static void replaceStr(string word, string word2, string > replace) > { > int i; > int x = 0, y, z = 0, l = 0; > char[] place = new char[word.Length]; > for (i = 0; i < word.Length; i++) > { > place[x] = word[i]; // fills the place Array > x++; > } > > for (y = 0; y < place.Length; y++) > { > > if (place[y] == word2[z]) / > { > > place[y] = replace[l]; // It only replaces the > first char > l++; > y++; > z++; > > } > > > } > > Console.WriteLine(place); > > } > be incrimenting y (y++) inside your if statement. You end up missing a character. After the change, try running your code on the following: string text = "Guten Morgen"; replaceStr(text,"Morning","Abend"); Console.WriteLine(text); You start to replace the word Morgen before completely checking if it equals Morning. On 29 Nov, 17:08, implement <tomico***@gmail.com> wrote:
Show quote > Hi all! You've double incremented y. There's a second issue you'll find when> > I try to code my own version of replace but it doesn't work. I hope > somebody can help me out. > It only replaces the first char!. > > Why I don't use the public string.replace function? Well I love > implementing string functions :-). > > I coded another function for replacing chars in a word and this > function works! > > Thank you for any help > > Use: > > string text = "Good Morning"; > replaceStr(text,"Morning","Evening"); > Console.WriteLine(text); > > Output: Good Eorning // Only the first char > > Here is the function > > public static void replaceStr(string word, string word2, string > replace) > { > int i; > int x = 0, y, z = 0, l = 0; > char[] place = new char[word.Length]; > for (i = 0; i < word.Length; i++) > { > place[x] = word[i]; // fills the place Array > x++; > } > > for (y = 0; y < place.Length; y++) > { > > if (place[y] == word2[z]) / > { > > place[y] = replace[l]; // It only replaces the > first char > l++; > y++; > z++; > > } > > } > > Console.WriteLine(place); > > } you try the test line with replaceStr("GoMod Morning", "Morning", "Evening"); On Nov 29, 11:08 am, implement <tomico***@gmail.com> wrote:
Show quote > Hi all! That's because y is incremented twice on each interation of the loop.> > I try to code my own version of replace but it doesn't work. I hope > somebody can help me out. > It only replaces the first char!. > > Why I don't use the public string.replace function? Well I love > implementing string functions :-). > > I coded another function for replacing chars in a word and this > function works! > > Thank you for any help > > Use: > > string text = "Good Morning"; > replaceStr(text,"Morning","Evening"); > Console.WriteLine(text); > > Output: Good Eorning // Only the first char > > Here is the function > > public static void replaceStr(string word, string word2, string > replace) > { > int i; > int x = 0, y, z = 0, l = 0; > char[] place = new char[word.Length]; > for (i = 0; i < word.Length; i++) > { > place[x] = word[i]; // fills the place Array > x++; > } > > for (y = 0; y < place.Length; y++) > { > > if (place[y] == word2[z]) / > { > > place[y] = replace[l]; // It only replaces the > first char > l++; > y++; > z++; > > } > > } > > Console.WriteLine(place); > > } Even after you fix that there's still some problems. First, it will replace things it should not be replacing. Second, strings are immutable so unless you return a new string that function is basically nothing more than a CPU consuming noop. On 29 Nov., 18:33, Brian Gideon <briangid***@yahoo.com> wrote:
Show quote > On Nov 29, 11:08 am, implement <tomico***@gmail.com> wrote: Thanks I fixed that with y but there other problems like you said. Any> > > > > > > Hi all! > > > I try to code my own version of replace but it doesn't work. I hope > > somebody can help me out. > > It only replaces the first char!. > > > Why I don't use the public string.replace function? Well I love > > implementing string functions :-). > > > I coded another function for replacing chars in a word and this > > function works! > > > Thank you for any help > > > Use: > > > string text = "Good Morning"; > > replaceStr(text,"Morning","Evening"); > > Console.WriteLine(text); > > > Output: Good Eorning // Only the first char > > > Here is the function > > > public static void replaceStr(string word, string word2, string > > replace) > > { > > int i; > > int x = 0, y, z = 0, l = 0; > > char[] place = new char[word.Length]; > > for (i = 0; i < word.Length; i++) > > { > > place[x] = word[i]; // fills the place Array > > x++; > > } > > > for (y = 0; y < place.Length; y++) > > { > > > if (place[y] == word2[z]) / > > { > > > place[y] = replace[l]; // It only replaces the > > first char > > l++; > > y++; > > z++; > > > } > > > } > > > Console.WriteLine(place); > > > } > > That's because y is incremented twice on each interation of the loop. > > Even after you fix that there's still some problems. First, it will > replace things it should not be replacing. Second, strings are > immutable so unless you return a new string that function is basically > nothing more than a CPU consuming noop.- Zitierten Text ausblenden - > > - Zitierten Text anzeigen - better implementation? I don't want to use the standard replace function implement wrote:
> Thanks I fixed that with y but there other problems like you said. Any In order to provide recommendations on better implementation, please > better implementation? I don't want to use the standard replace > function help us understand why the standard replace function is not workable. -- Tom Porterfield On 29 Nov., 18:52, Tom Porterfield <tppor***@mvps.org> wrote:
> implement wrote: The standard replace function is nice but I want to look behind the> > Thanks I fixed that with y but there other problems like you said. Any > > better implementation? I don't want to use the standard replace > > function > > In order to provide recommendations on better implementation, please > help us understand why the standard replace function is not workable. > -- > Tom Porterfield screen how these things are implemented. I tried Reflector but it doesn't show me the whole implementations. So I try to build these functions again. Just for fun :-)
Show quote
"implement" <tomico***@gmail.com> wrote in message You should start by describing the exact behavior of the function.news:6d127aa9-b57b-4f05-86a3-0c240ee270eb@s12g2000prg.googlegroups.com... > On 29 Nov., 18:52, Tom Porterfield <tppor***@mvps.org> wrote: >> implement wrote: >> > Thanks I fixed that with y but there other problems like you said. Any >> > better implementation? I don't want to use the standard replace >> > function >> >> In order to provide recommendations on better implementation, please >> help us understand why the standard replace function is not workable. >> -- >> Tom Porterfield > > The standard replace function is nice but I want to look behind the > screen how these things are implemented. I tried Reflector but it > doesn't show me the whole implementations. So I try to build these > functions again. Just for fun :-) The code you gave would be described as: "looks through the input until it starts finding the findtext, then starts replacing with the newtext", which is not what the built-in replace does. Maybe (as others have alluded) instead of reinventing the wheel, you could
look into the REGEX replace method that will allow you to do lots of this kind of stuff without the tribulations of time - consuming custom methods? --Peter "Inside every large program, there is a small program trying to get out." http://www.eggheadcafe.com http://petesbloggerama.blogspot.com http://www.blogmetafinder.com Show quote "implement" wrote: > Hi all! > > I try to code my own version of replace but it doesn't work. I hope > somebody can help me out. > It only replaces the first char!. > > Why I don't use the public string.replace function? Well I love > implementing string functions :-). > > I coded another function for replacing chars in a word and this > function works! > > Thank you for any help > > Use: > > string text = "Good Morning"; > replaceStr(text,"Morning","Evening"); > Console.WriteLine(text); > > Output: Good Eorning // Only the first char > > Here is the function > > public static void replaceStr(string word, string word2, string > replace) > { > int i; > int x = 0, y, z = 0, l = 0; > char[] place = new char[word.Length]; > for (i = 0; i < word.Length; i++) > { > place[x] = word[i]; // fills the place Array > x++; > } > > for (y = 0; y < place.Length; y++) > { > > if (place[y] == word2[z]) / > { > > place[y] = replace[l]; // It only replaces the > first char > l++; > y++; > z++; > > } > > > } > > Console.WriteLine(place); > > } > |
|||||||||||||||||||||||