My own version of replace

I

implement

implement wrote:
implement wrote:
The Debugger stops at if (place[y] == word2[z]) with an
IndexOutOfRangeExecption
It appears you have changed the code since your original post. Can you
supply your current code as well as the inputs that you are passing into
your replaceStr method?
--
Tom Porterfield
Use:
string text = "Programming is fun";
replaceStr(text,"is","bla");
Debugger output:
place[x] contains 18 Elements, x too. place[x] contains these elements
in array order
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
'P' 'r' 'o' 'g' 'r' 'a' 'm' 'm' 'b' 'n' 'g' ' ' 'i'
'l' ' ' 'f' 'u' 'n'
Now it goes to the next for loop
for (y = 0; y < place.Length; y++ ) // Debugger output: y = 14
Next:
if (place[y] == word2[z]) // here it crashes with an exception
18 14
What is 18 here??? At the point of the crash, y is 14 and z is 2.
I don't know why y is only 14. It should be 18 too?
It's 14 because it's not finished looping all the way through place. It
won't be 18 until the loop is finished, but you are crashing before that
happens.
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;
x++;
}
for (y = 0; y < place.Length; y++ )
{
Console.WriteLine(y);
if (place[y] == word2[z])
{
place[y] = replace[l];
l++;
z++;
}
}
Console.WriteLine(place);
}
When I first looked at your code I suspected this would happen, but
didn't fire it up to confirm as others were on it. The issue for you is
that word2 is shorter than replace. You are incrementing the counter
for both word2 and replace everytime you do a substitution.
First of all, this will never work like string.Replace, but maybe that's
no longer a goal. As you can see from the output you pasted above, you
replaced the 'i' in "Programming" because it matched 'i' in "is" with
the 'b' in "bla", and then incremented the indexer for both "is" and
"bla". So now your code started looking for a match on 's' ("is"[1]).
It found a match for that in the 's' of "is" in your second word of
"Programming is fun" and replaced it with the 'l' in "bla" ("bla"[1]).
So you now have "Programmbng ls fun". You then again incremented the
indexer for both "is" and "bla" which means the next time through the
loop you are comparing place[14] with word2[2]. Since word2 = "is", it
has no character at index 2 (remember 0 based index) so you get your
argument out of range exception.
Hope this helps.

Thanks it helps but I don't know how can I change my false version
into a true one :-(.- Zitierten Text ausblenden -
- Zitierten Text anzeigen -

I think it's always easier replacing single chars as grouped
chars(strings)- Zitierten Text ausblenden -

- Zitierten Text anzeigen -


I mean it's always easier replacing single chars than grouped
chars(strings)
 
T

Tom Porterfield

implement said:
I mean it's always easier replacing single chars than grouped
chars(strings)

Maybe so, but that isn't what string.Replace does so if you are trying
to reproduce the behavior of that you'll need to find the match across
the group and replace the group, taking into account that the number of
elements in the replacement group might be more or less than the number
of elements they are replacing. string.Replace also supports passing
null or an empty string as the "replace with" value, with the result
being that matched items are removed from the original string.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top