4711
4711

Reputation: 21

Filling an array with fillers in 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...)

Upvotes: 2

Views: 709

Answers (6)

EpicKip
EpicKip

Reputation: 4033

int fixedLength = 20;
string mockupInput = "bladjiea";
string filler = "-";
While(mockupInput.Length < fixedLength)
{
    mockupInput += filler;
}

This is easy beginner code that should work.

Upvotes: 1

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

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();

Upvotes: 5

Happy Bird
Happy Bird

Reputation: 1132

I think this code should work for you.

Console.WriteLine("Please type in your company number!");
string companyNr = Console.ReadLine();

Console.WriteLine("Type a filler");
string filler= Convert.ToChar(Console.ReadLine());

string fill = companyNr.PadLeft(20,filler);
// or use string fill = companyNr.PadRight(20,filler);

Welcome to StackOverflow. I am also a beginner :)

Upvotes: 2

Roman
Roman

Reputation: 12171

You can write some method (or extension method):

string AppendString(string str, int count, char filler, bool fromStart)
{
    return fromStart ? str.PadLeft(count, filler) : str.PadRight(count, filler);
}

Upvotes: 2

Mats391
Mats391

Reputation: 1209

Since you are getting strings as an input you can use string padding. Look here: Padding Strings in the .NET Framework

Upvotes: 2

Stefan
Stefan

Reputation: 17648

Lets make this a contest on who will write most beautiful code:

I'll start with the most trivial, brute method XD.

If you have the number:

int fillLength = 20 - companyNr.Length; //for example, 20 chars total.

You can fill a StringBuilder in a loop:

var sb = new StringBuilder()
for (int c = 0; c < fillLength; c++)
    sb.Append(filler);

string result = companyNr  + sb.ToString();

Upvotes: 0

Related Questions