我希望用户首先输入代码(例如fkuk3463kj).数组限制为20.其余的必须填充填充符.客户将使用哪种填充物(例如#,t,z,7,_,0)是他自己的选择,他将要求在代码问题之后的开头定义它.
(提示:之后(或如果可能的话)我必须决定(完成顾客的愿望)填料是否必须在开头或结尾.(例如:fkuk3463kj ######### #或########## fkuk3463kj)
现在我不知道如何实现这一点.我知道,这并不困难,但我不明白!我所有的尝试都不是真的成功.
有人能帮助我吗?这将是完美的!而且很多提前!
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]; . . . . .
(请原谅我的英文...)
据我所见,你正在与string
:
// Trim: let's trim off leading and trailing spaces: " abc " -> "abc" string companyNr = Console.ReadLine().Trim();
你想要的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();