简体   繁体   English

使用C#中的填充填充数组

[英]Filling an array with fillers in C#

I want the user to first type a code (eg fkuk3463kj) The array is limited to 20. The rest must be filled with fillers. 我希望用户首先输入代码(例如fkuk3463kj)。数组限制为20.其余的必须填充填充符。 Which filler the customer will use (eg #, 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. 客户将使用哪种填充物(例如#,t,z,7,_,0)是他自己的选择,他将要求在代码问题之后的开头定义它。

(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) (提示:之后(或如果可能的话)我必须决定(完成顾客的愿望)填料是否必须在开头或结尾。(例如:fkuk3463kj ######### #或########## 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...) (请原谅我的英文...)

As far as I can see, you're working with string : 据我所知,你正在使用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): 你想要Pad一些char length (在你的情况下为20 ):

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

Since you are getting strings as an input you can use string padding. 由于您将字符串作为输入,因此可以使用字符串填充。 Look here: Padding Strings in the .NET Framework 请看这里: .NET Framework中的填充字符串

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

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. 欢迎来到StackOverflow。 I am also a beginner :) 我也是初学者:)

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

This is easy beginner code that should work. 这是易于使用的初学者代码。

Lets make this a contest on who will write most beautiful code: 让我们参加一个关于谁将编写最美丽代码的竞赛:

I'll start with the most trivial, brute method XD. 我将从最琐碎,粗野的方法XD开始。

If you have the number: 如果你有这个号码:

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

You can fill a StringBuilder in a loop: 您可以在循环中填充StringBuilder

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

string result = companyNr  + sb.ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM