10 Replies - 734 Views - Last Post: 12 February 2011 - 06:50 AM Rate Topic: -----

#1 Milto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 23
  • View blog
  • Posts: 139
  • Joined: 10-February 11

Help Replacing characters in a string

Posted 10 February 2011 - 05:03 PM

Hi, I'm new to programming and I really need help with this:
I'm trying to make a program that finds and replaces letters in a string.

I'm assuming this involves arrays, which I HATE and have difficulty understanding outside of console applications.

All I need to know is
-how to write out the list of letters I want to replace and what I want to replace them with
-the method used to find and replace these characters in the string

Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Help Replacing characters in a string

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,467
  • Joined: 29-May 08

Re: Help Replacing characters in a string

Posted 10 February 2011 - 05:14 PM

Array are easy.
Think of an array as an hotel with rooms. Each room has a number that uniquely identifies it eg ROOM NUMBER.

// Hotel with 100 rooms, in which only Int can stay in.
Int[] Hotel = New Int[100];
// Porter put guest called 123 into roomnumber 42
int Room_Number=42;
Hotel[Room_Number]=123;
// Porter looks to see what in room.
int WhatsInRoom = Hotel[Room_Number];
// 123 is still in room 42

This post has been edited by AdamSpeight2008: 10 February 2011 - 05:23 PM

Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: Help Replacing characters in a string

Posted 10 February 2011 - 05:17 PM

String.Replace
Was This Post Helpful? 0
  • +
  • -

#4 Milto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 23
  • View blog
  • Posts: 139
  • Joined: 10-February 11

Re: Help Replacing characters in a string

Posted 11 February 2011 - 04:08 PM

Thanks for the replies but how can i utilize the String.Replace method outside of the console application so it changes certain characters in a textbox at the click of a button.

A basic example would help a lot.
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: Help Replacing characters in a string

Posted 11 February 2011 - 04:29 PM

A basic example would be incredibly basic. We expect you to at least try to solve your own problems here; we don't just go write code for people.

So, what part are you having an issue with? Do you know how to execute code when you click a button? If you know that much, then it's really no different than a console application. If you don't, just double-click the button in the designer, and it'll make a method for you that will execute when you click the button.
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,467
  • Joined: 29-May 08

Re: Help Replacing characters in a string

Posted 11 February 2011 - 04:47 PM

Console Applications are in my opinion the easiest way to learn the basics of a language. How you use an array in a console is exactly the same way you use one in a form based one. Once you've grasp the basic, move on to a form.
Was This Post Helpful? 0
  • +
  • -

#7 Milto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 23
  • View blog
  • Posts: 139
  • Joined: 10-February 11

Re: Help Replacing characters in a string

Posted 11 February 2011 - 06:52 PM

I know how to write code that will execute when I click a button.
Sorry if you misunderstood by request for a "basic example".
By basic example I meant an instance where String.Replace is used. Just something to analyze and learn from.

And thanks AdamSpeight, console apps are really good for learning arrays in depth.
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: Help Replacing characters in a string

Posted 11 February 2011 - 06:59 PM

The MSDN almost always has examples for using classes and methods:

http://msdn.microsof...y/fk49wtc1.aspx
Was This Post Helpful? 0
  • +
  • -

#9 Milto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 23
  • View blog
  • Posts: 139
  • Joined: 10-February 11

Re: Help Replacing characters in a string

Posted 12 February 2011 - 06:21 AM

Thanks for the link. It helped me a lot. I just can't understand what's wrong with this code:

public string letterReplace(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;
}

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        string[] Old = { "a", "b", "c" };
        string[] New = { "1", "2", "3" };
        textBox1.Text = letterReplace(textBox1.Text, Old, New);
    }
    catch (Exception excMsg)
    {
        MessageBox.Show(excMsg.Message.ToString(), "Error");
    }
}



It looks like it could work, but no results when I click button1.
(the goal is to replace those particular letters written in textBox1 with those numbers respectively
(a becomes 1, b becomes 2, c becomes 3))
Was This Post Helpful? 0
  • +
  • -

#10 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: Help Replacing characters in a string

Posted 12 February 2011 - 06:37 AM

Are you sure it doesn't work? It seems to work fine for me...what doesn't work about it? You have registered to listen for the click event of the button haven't you?

Like this:

this.button1.Click += new System.EventHandler(this.button1_Click);

This post has been edited by CodingSup3rnatur@l-360: 12 February 2011 - 06:49 AM

Was This Post Helpful? 0
  • +
  • -

#11 Milto  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 23
  • View blog
  • Posts: 139
  • Joined: 10-February 11

Re: Help Replacing characters in a string

Posted 12 February 2011 - 06:50 AM

Oh I understand now. It does work, I'm not sure what the original problem was though.

Well, thanks everyone that posted. Problem Solved! :rockon:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1