5 Replies - 366 Views - Last Post: 20 August 2010 - 11:47 AM Rate Topic: -----

#1 PE4ce  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-August 10

generate text with for loop from input text

Posted 20 August 2010 - 08:51 AM

hi all

I want to do a program that see if the text have "$" then he replace it with numbers from 0 to 10 or whatever then add the text to listbox
for example
the user enter : abc$abc
the program will add it into listbox like this:
abc0abc
abc1abc
abc2abc
.
.
.
and so on

string n="";
            int l;

            char [] nu1  = textBox1.Text.ToCharArray();
            //string sr;
             //sr= textBox1.Text;
            for (int i = 0; i < textBox1.Text.Length; i++)
            {
                if (nu1[i] == '$')
                {
    
               n = Convert.ToString(nu1[i]);
                }
                //s=Convert.ToString(nu1[i]);
                l = Convert.ToInt32(n);
                before += nu1[i];
                for (int j = 0; j < 10; j++)
                {
                    textBox2.Text = before + j;
                }
            }
            listBox1.Items.Add(before );
            
            
        }



Is This A Good Question/Topic? 0
  • +

Replies To: generate text with for loop from input text

#2 Curtis Rutland  Icon User is online

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


Reputation: 4464
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Re: generate text with for loop from input text

Posted 20 August 2010 - 09:00 AM

Quote

I want to do a program that see if the text have "$" then he replace it with numbers from 0 to 10 or whatever then add the text to listbox


Go ahead. What's stopping you?

If you have an actual question or code problem, please say what it is.
Was This Post Helpful? 0
  • +
  • -

#3 Ahmedn1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 23
  • View blog
  • Posts: 556
  • Joined: 04-August 09

Re: generate text with for loop from input text

Posted 20 August 2010 - 09:07 AM

View PostPE4ce, on 20 August 2010 - 07:51 AM, said:

hi all

I want to do a program that see if the text have "$" then he replace it with numbers from 0 to 10 or whatever then add the text to listbox
for example
the user enter : abc$abc
the program will add it into listbox like this:
abc0abc
abc1abc
abc2abc
.
.
.
and so on

string n="";
            int l;

            char [] nu1  = textBox1.Text.ToCharArray();
            //string sr;
             //sr= textBox1.Text;
            for (int i = 0; i < textBox1.Text.Length; i++)
            {
                if (nu1[i] == '$')
                {
    
               n = Convert.ToString(nu1[i]);
                }
                //s=Convert.ToString(nu1[i]);
                l = Convert.ToInt32(n);
                before += nu1[i];
                for (int j = 0; j < 10; j++)
                {
                    textBox2.Text = before + j;
                }
            }
            listBox1.Items.Add(before );
            
            
        }




try this code


string n = textBox1.Text;
            if (n.IndexOf('$') != -1)
            {
                for (int i = 0; i < n.Length; i++)
                {
                    listBox1.Items.Add(n.Replace('$', Convert.ToChar(i.ToString())));
                }
            }
            
            
        }


Was This Post Helpful? 1
  • +
  • -

#4 PE4ce  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-August 10

Re: generate text with for loop from input text

Posted 20 August 2010 - 09:48 AM

View PostAhmedn1, on 20 August 2010 - 08:07 AM, said:

View PostPE4ce, on 20 August 2010 - 07:51 AM, said:

hi all

I want to do a program that see if the text have "$" then he replace it with numbers from 0 to 10 or whatever then add the text to listbox
for example
the user enter : abc$abc
the program will add it into listbox like this:
abc0abc
abc1abc
abc2abc
.
.
.
and so on

string n="";
            int l;

            char [] nu1  = textBox1.Text.ToCharArray();
            //string sr;
             //sr= textBox1.Text;
            for (int i = 0; i < textBox1.Text.Length; i++)
            {
                if (nu1[i] == '$')
                {
    
               n = Convert.ToString(nu1[i]);
                }
                //s=Convert.ToString(nu1[i]);
                l = Convert.ToInt32(n);
                before += nu1[i];
                for (int j = 0; j < 10; j++)
                {
                    textBox2.Text = before + j;
                }
            }
            listBox1.Items.Add(before );
            
            
        }




try this code


string n = textBox1.Text;
            if (n.IndexOf('$') != -1)
            {
                for (int i = 0; i < n.Length; i++)
                {
                    listBox1.Items.Add(n.Replace('$', Convert.ToChar(i.ToString())));
                }
            }
            
            
        }



thanks ahmad this is working
Was This Post Helpful? 0
  • +
  • -

#5 Ahmedn1  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 23
  • View blog
  • Posts: 556
  • Joined: 04-August 09

Re: generate text with for loop from input text

Posted 20 August 2010 - 11:05 AM

glad to help
Was This Post Helpful? 0
  • +
  • -

#6 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: generate text with for loop from input text

Posted 20 August 2010 - 11:47 AM

Generally, you could do it with the help of a foreach loop, knowing there is a multitude of the items you are working with:

// Sample array
string[] set = new string[] { "abc$def", "ghi$jkl", "SSS" };

List<string> list = new List<string>();
int i = 0;
foreach (string unit in set)
{
    list.Add(unit.Replace("$", i.ToString()));
    i++;
}



However, since you only have a single TextBox control to accept user input, you can always just use this:

list.Add(textBox1.Text.Replace("$", i.ToString()));
i++;


Note: i is an incrementing variable.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1