Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filling an array with fillers in C#

Tags:

arrays

c#

I want the user to first type a code (e.g. fkuk3463kj) The array is limited to 20. The rest must be filled with fillers. Which filler the customer will use (e.g. #, t, z,7,_,0) is his own choice and he will asked to define it at the beginning right after the question for the code.

(hint: afterwards (or if possible directly) I have to decide (to complete the wish of customer) whether the filler has to be at the beginning or at the end. (for example: fkuk3463kj########## or ##########fkuk3463kj)

Now I don't know how to implement this. I know, that it's not that difficult, but I don't get it! All my tryings were not really succesful.

Could anybody help me? This would be perfect! And many thx in advance!

Console.WriteLine("Please type in your company number!");
string companyNr = Console.ReadLine();
string[] CNr = new string[companyNr.Length];
Console.WriteLine("Type a filler");
string filler= Convert.ToString(Console.ReadLine());
string[] fill = new string[filler.Length];

.
.
.
.
.

(please pardon my english...)

like image 758
4711 Avatar asked Feb 04 '26 00:02

4711


1 Answers

As far as I can see, you're working with string:

 // Trim: let's trim off leading and trailing spaces: "  abc " -> "abc"
 string companyNr = Console.ReadLine().Trim();

which you want to Pad with some char up to the length length (20 in your case):

 int length = 20;

 string filler = Console.ReadLine().Trim();
 // padding character: either provided by user or default one (#) 
 char pad = string.IsNullOrEmpty(filler) ? '#' : filler[0];

 // shall we pad left: "abc" -> "##abc" or right: "abc" -> "abc##"
 // I have to decide (to complete the wish of customer) 
 //TODO: whether the filler has to be at the beginning or at the end
 bool leftPad = true;

 string result = leftPad
   ? companyNr.PadLeft(length, pad) 
   : companyNr.PadRight(length, pad);

 // in case you want a char array
 char[] array = result.ToCharArray();
 // in case you want a string array
 string[] strArray = result.Select(c => c.ToString()).ToArray();
like image 125
Dmitry Bychenko Avatar answered Feb 05 '26 13:02

Dmitry Bychenko