0

I have a small problem using PadLeft and PadRight. So I have in my code that the user can input the character they want to use for padding and how many characters they wanna put in. Like this:

String StartString;
int AmountOfCharacters;
Char PadCharacter;

StartString = TextBoxString.Text(Lawnmower)
AmountOfCharacters = Convert.ToInt32(TextBoxAmountofCharacters.Text)    (Lets   Say 5)
PadCharacter = Convert.ToChar(TextBoxPadCharacter.Text)(Lets use *)

So then later i have put.

Padding = String.PadLeft(AmountOfCharacters,PadCharacter)

Now the problem I have when I run the code as I have it above it doesn't do anything. It just gives me as text lawnmower without any **** attatched. Do I have to change something in my code to make it work or am I using the wrong variables for this? Because when I use the PadCharacter as a String to I get a error message

Cannot implicitly convert String to char.

Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
Blasje
  • 37
  • 1
  • 1
  • 4
  • I have been Looking further in my code and for some reason during the convert or somewhere . 48 '*' gets made from the Variable im using. So i guess the 48 in there is whats screwing up my Pad. – Blasje Mar 18 '16 at 15:15

3 Answers3

1

You misunderstand how PadLeft() works. The length you specify as a parameter (in your case AmountOfCharacters) does not specify how many characters you want added but how many characters the string should have at the end (at least).

So when you specify the string "Lawnmower" and AmountOfCharacters = 5, nothing will happen because the word Lawnmower is already more than 5 characters long.

NineBerry
  • 26,306
  • 3
  • 62
  • 93
1

If StartString charecter count is less then AmountOfCharacters you can see stars infront of StartString. The number of stars will be [AmountOfCharacters] - [StartString Character Count]

aaltintop
  • 188
  • 9
0

String is a sequence of characters, but not itself a character - that's why Convert.ToChar fails with an exception. Try TextBoxPadCharacter[0] to get the first character of user input. You will also need to verify that the input is non-empty.

Eugene Shvets
  • 4,561
  • 13
  • 19