C# tutorials
14. Replace with two arrays

Note: While reading this page, bear in mind that I'm no computer expert and that the text below may be partly inaccurate. If you find errors or have proposals for improvements, please send me a message and help make this a better page for the benefit of future visitors. To the left, there are links to more C# tutorials.

To complete this tutorial, follow these instructions:

1. Open Visual C# 2010 Express.

2. Click on New Project in the File menu.

3. Choose Windows Forms Application if that option isn't already chosen, change the name if you like and click on OK.

4. Point at Toolbox in the left margin and click on the Auto Hide icon at the top of the window (to keep the window to stay open and easier to work with).

5. Drag a Button to Form 1.

6. Drag a Rich Textbox to Form 1. Resize the textbox if you like.

7. Double-click on the button. The file Form1.cs will open with the caret ready to write code that will run when button 1 is clicked.

8. Write or paste this code where the caret is:

  string[] Old = { "soup", "milk", "candy" };
  string[] New = { "pizza", "beer", "bananas" };
  richTextBox1.Text = mkReplace(richTextBox1.Text, Old, New);
catch (Exception excMsg)
  MessageBox.Show(excMsg.Message.ToString(), "Error");

9. Write or paste this code right above the line private void button1_Click(object sender, EventArgs e):

public string mkReplace(string str, string[] Old, string[] New)
  for (int i = 0; i < Old.Length; i++)
    if (i < New.Length)
      str = str.Replace(Old[i], New[i]);
  return str;

10. Press F5 to start debugging the program.

11. Write or paste this text to the textbox: I like soup, milk and candy.

12. Click on the button. The text should change to: I like pizza, beer and bananas.

13. Remove bananas from the string New and repat step 10-12. The text should change to: I like pizza, beer and candy.

Comment on the first code snippet

Line 3-5: Replace soup with pizza, milk with beer and candy with bananas or, if you're doing step 13, replace soup with pizza and milk with beer.

Comments on the second code snippet

Line 3-9: Replace the first value in the array Old with the first value in the array New, replace the second value in the array Old with the second value in the array New, etc. until the end of the array Old is reached or, if the array New is shorter than the array Old, until the end of the array New is reached.

Line 3 & 5: Note that < (less than) is used instead of <= (less than or equal to), because array index starts with 0 and Length with 1.

Line 5: Check that the int i is less than the length of the array New.

Line 7: The string str is updated each time the loop is run so that replacements are kept.

Would you like to comment on this page or some other page? Send an email to mats.kristiansson.skovde@gmail.com or a letter to Mats Kristiansson, Timmervägen 3A, 541 64 Skövde, Sweden with the title of the page you want to comment on, your comment and your name or a pseudonym.