473,394 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 2049
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: laurie | last post by:
Hi all, I'm trying to help out a friend who has inherited a client with a PHP shopping cart application. Neither of us know PHP, but I've been muddling my way through, trying to get these old...
0
by: ianstratford | last post by:
I just released a new version of the free Microsoft Access find and replace add-in utility (version 1.5). Search and replace (optionally) any string in tables, queries, macros, forms, reports...
0
by: Neil Sargent | last post by:
This is a general posting of how I fixed a problem created by using the Access 97 ODE Setup Wizard on a Windows XP machine. I hope it helps anyone who comes across the problem saves them the 3 days...
5
by: Tara via AccessMonster.com | last post by:
Hi there - I'll do my best to explain my dilema. I'm using Access 2000. In this database, there is one table with about 150 columns of information, and 206 rows. There are numerous queries and...
1
by: Jim | last post by:
Hi. I would like to validate the version string that I get from FileVersionInfo. Is there a way via the object model to do this? I am currently doing this: FileVersionInfo fileVersionInfo =...
5
by: Bazza Formez | last post by:
Greetings... I have an odd problem to do with replacing a DLL. My ASP.NET application makes use of a 3rd party charting component. This component is supplied as a DLL file. The component...
8
by: Rak | last post by:
I am looking for a way to programatically change the .net version of the virtual directory that I am creating within a aspx page. As part of creating a new customer in my asp.net 2 application, it...
2
by: ssp | last post by:
Hello there, I am trying to do very simple thing in trying to return results from an Oracle (10g) Stored Procedure using the Enterprise Library June 2005 Version. The stored procedure first:...
6
by: John | last post by:
I have written a VB.NET DLL that is called by a third party program. If I make any changes in the DLL and then try to replace my DLL file the third party program will abort, saying that the DLL...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.