473,770 Members | 4,553 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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","Eve ning");
Console.WriteLi ne(text);

Output: Good Eorning // Only the first char

Here is the function

public static void replaceStr(stri ng 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.WriteLi ne(place);

}
Nov 29 '07
12 2087
implement wrote:
The Debugger stops at if (place[y] == word2[z]) with an
IndexOutOfRange Execption
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
3271
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 scripts working on a new server with the most recent version of PHP. I've pretty much taken care of all the various errors that were popping up. Most only pointed out out non-fatal undefined or assumed variables. I've been able to cure most of...
0
1448
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 and modules in Access databases. The tool will now search and replace Report Group Levels (Thank you Judy). The new version also searches macros (logs results but does not
0
2016
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 its taken me to sort out. The application generates the following error: "Incompatible version of the RPC stub" This occurs when trying to run Outlook to send an email. However I expect it would occur for any operation which uses...
5
1897
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 reports. I've set up input forms for staff to enter data easily. There are 8 staff members. My boss wants to ensure that data entered into this database is correct right off the bat. She's asking for a program or code to do the following:
1
6215
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 = FileVersionInfo.GetVersionInfo(filePath); string fileVersion = fileVersionInfo.FileVersion;
5
5101
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 has worked well for us, but now I am having real problems trying to replace the DLL with a new version.
8
7775
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 automatically creates a virtual directory and configures it. I am using the DirectoryServices.DirectoryEntry class in C# to do this. I am unable to set the .Net version of the virtual directory from 1.1 to version 2. All searches led me to the...
2
4925
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: ============================================ CREATE OR REPLACE PROCEDURE AGENCYSELECTALL(p_cursor OUT SelectPackage.SelectCursor)
6
2597
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 version number does not match to the DLL that it was linked against. How can I recompile my DLL without incrementing the version number? I don't see where this "version number" is stored in the project. The version number, under the "Assembly...
0
9602
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10237
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10071
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10017
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9882
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8905
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3987
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.