0

Hi guys,

Sorry the title is vague. Couldn't come up with one.

Ok so I have a int variable which is, let's say, of the value 5.

Now I want to create a string which contains underscores, so: "_____" the amount of underscores is the same value of the integer. So if the int variable is of value 5 then the string should have 5 underscores, like this: "_____"

Any idea?

3
Contributors
10
Replies
11
Views
5 Years
Discussion Span
Last Post by Momerath
Featured Replies
  • Something lke that? [CODE] private void method() { const int value = 5; string tag = "_"; string item = ""; for (int i = 0; i < value; i++) item += tag; MessageBox.Show("You have chosen " + value.ToString() + " underscores.\nThe result is: \"" + item + "\""); } … Read More

  • Sure you cannot. This will not do: [CODE] for (int i = 0; i == length; i++) { lblWord.Text = lblWord.Text + "_"; }[/CODE] You have to do the loop which will go through it: [CODE] for (int i = 0; i < length; i++) { lblWord.Text += "_"; //this … Read More

  • 1

    [code]String s = string.Empty.PadLeft(6, '-');[/code] Replace the six with your variable. No loops required. Read More

1

Something lke that?

private void method()
        {
            const int value = 5;
            string tag = "_";
            string item = "";
            for (int i = 0; i < value; i++)
                item += tag;
            MessageBox.Show("You have chosen " + value.ToString() + " underscores.\nThe result is: \"" + item + "\"");
        }

Mitja

0

I think i've done it. I did this:

word = myArray[random.Next(0,myArray.Length)];
            length = word.Length;

            Label lblWord = new Label();

            lblWord.AutoSize = true;
            lblWord.Location = new System.Drawing.Point(24, 91);
            lblWord.Name = "lblWord";
            lblWord.Size = new System.Drawing.Size(27, 13);
            lblWord.TabIndex = 1;
            lblWord.Text = "";

            for (int i = 0; i == length; i++)
            {
                lblWord.Text = lblWord.Text + "_";
            }

Where the string is actually a label and i'm drawing the label on to the form.

CODING IS FUN :D (Hi Mitja!)

0

Hi again. I added some stuff:

private void Form1_Load(object sender, EventArgs e)
        {
            lblOne.Visible = false;
            lblTwo.Visible = false;
            lblThree.Visible = false;
            lblFour.Visible = false;

            word = myArray[random.Next(0,myArray.Length)];
            length = word.Length;

            Label lblWord = new Label();

            lblWord.AutoSize = true;
            lblWord.Location = new System.Drawing.Point(24, 91);
            lblWord.Name = "lblWord";
            lblWord.Size = new System.Drawing.Size(27, 13);
            lblWord.TabIndex = 1;
            lblWord.Text = "";

            for (int i = 0; i == length; i++)
            {
                lblWord.Text = lblWord.Text + "_";
            }

            Controls.Add(lblWord);
            lblWord.Visible = true;


        }

But I still don't see the label appearing on to the form. What am I doing wrong. (Brb Family dinner).

1

Sure you cannot. This will not do:

for (int i = 0; i == length; i++)
 {
      lblWord.Text = lblWord.Text + "_";
 }

You have to do the loop which will go through it:

for (int i = 0; i < length; i++)
 {
      lblWord.Text += "_";  //this will do it too :)
 }

Mitja

0

Still....When I press F5 and run the program, the label doesn't appear.....

0

Nope - the length is definitely working. I did a quick test and made a new GUI to make sure the length and the array etc are working. It does:

private void Form1_Load(object sender, EventArgs e)
        {
            word = myArray[random.Next(0,myArray.Length)];
            length = word.Length;

            label1.Text = word.ToString();
            textBox1.Text = length.ToString();
        }

Works perfectly. The label1 and textbox1 on the GUI work as they should...So why isn't my initial program working? AM i missing something?

0

OMG IT WORKS! W000H00!!!!! I forgot to change thr for loop -_-' STUPID ME! :

for (int i = 0; i <= length; i++)
            {
                lblWord.Text += "_ ";
            }

Excellent. Thanks!

1
String s = string.Empty.PadLeft(6, '-');

Replace the six with your variable. No loops required.

Edited by Momerath: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.