471,071 Members | 1,319 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,071 software developers and data experts.

My own version of replace

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);

}
Nov 29 '07 #1
12 1955
On 29 Nov, 17:08, implement <tomico...@gmail.comwrote:
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);

}
You've double incremented y. There's a second issue you'll find when
you try the test line with

replaceStr("GoMod Morning", "Morning", "Evening");

Nov 29 '07 #2


"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);

}
I think you are going to have more problems than this, but, you should not
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.
Nov 29 '07 #3
On Nov 29, 11:08 am, implement <tomico...@gmail.comwrote:
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.
Nov 29 '07 #4
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

"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);

}
Nov 29 '07 #5
On 29 Nov., 18:33, Brian Gideon <briangid...@yahoo.comwrote:
On Nov 29, 11:08 am, implement <tomico...@gmail.comwrote:


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 -
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

Nov 29 '07 #6
On Nov 30, 7:26 am, implement <tomico...@gmail.comwrote:
My function works, but only if "word" has 11 chars? Why? If more chars
then I got an System.IndexOutOfRangeException.
You'll need to use a debugger and step through the code. Have you
done that yet?
Nov 30 '07 #7
On 30 Nov., 15:13, Brian Gideon <briangid...@yahoo.comwrote:
On Nov 30, 7:26 am, implement <tomico...@gmail.comwrote:
My function works, but only if "word" has 11 chars? Why? If more chars
then I got an System.IndexOutOfRangeException.

You'll need to use a debugger and step through the code. Have you
done that yet?
Yes I used the debugger and it shows me that the place[x] array has 13
elements but y starts with and is 12. I don't know why
Nov 30 '07 #8
On Nov 30, 8:20 am, implement <tomico...@gmail.comwrote:
On 30 Nov., 15:13, Brian Gideon <briangid...@yahoo.comwrote:
On Nov 30, 7:26 am, implement <tomico...@gmail.comwrote:
My function works, but only if "word" has 11 chars? Why? If more chars
then I got an System.IndexOutOfRangeException.
You'll need to use a debugger and step through the code. Have you
done that yet?

Yes I used the debugger and it shows me that the place[x] array has 13
elements but y starts with and is 12. I don't know why
First, I don't see how the place array can have 13 elements since
"Good Morning" is only 12 characters. Have you changed your code?

Second, you're going to have to be more precise. The variable y
starts with...what? At what point is its value 12? What line is
throwing the IndexOutOfRangeException.

If you've stepped through it in a debugger it should be easy to
identify why the exception is being generated. Is it because you're
indexing into the place array with a value greater than place.Length -
1? Remember, arrays are 0 based.
Nov 30 '07 #9
On 30 Nov., 15:59, Brian Gideon <briangid...@yahoo.comwrote:
On Nov 30, 8:20 am, implement <tomico...@gmail.comwrote:
On 30 Nov., 15:13, Brian Gideon <briangid...@yahoo.comwrote:
On Nov 30, 7:26 am, implement <tomico...@gmail.comwrote:
My function works, but only if "word" has 11 chars? Why? If more chars
then I got an System.IndexOutOfRangeException.
You'll need to use a debugger and step through the code. Have you
done that yet?
Yes I used the debugger and it shows me that the place[x] array has 13
elements but y starts with and is 12. I don't know why

First, I don't see how the place array can have 13 elements since
"Good Morning" is only 12 characters. Have you changed your code?

Second, you're going to have to be more precise. The variable y
starts with...what? At what point is its value 12? What line is
throwing the IndexOutOfRangeException.

If you've stepped through it in a debugger it should be easy to
identify why the exception is being generated. Is it because you're
indexing into the place array with a value greater than place.Length -
1? Remember, arrays are 0 based.
The Debugger stops at if (place[y] == word2[z]) with an
IndexOutOfRangeExecption
Nov 30 '07 #10
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
Nov 30 '07 #11
implement wrote:
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 :-)
If it is the implementation that you are interested in, the following
downloads might be of value:

Shared Source Common Language Infrastructure 1.0 Release
http://www.microsoft.com/downloads/d...6-8DD34C6292F0

Shared Source Common Language Infrastructure 2.0 Release
http://www.microsoft.com/downloads/d...7-3121B4F51D4D
--
Tom Porterfield
Nov 30 '07 #12
implement wrote:
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.
--
Tom Porterfield
Dec 1 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Tara via AccessMonster.com | last post: by
5 posts views Thread by Bazza Formez | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.