打印带字符的String 周围加了很多空格
Print String with characters around adding a lot of spaces
好吧,我正在复习 C# 并尝试制作一个简单的打印机。例如,您键入一个单词或短语并在其周围打印 = 符号。但是,调试它时,我注意到当我超过字符限制时,它会破坏样式。例如,当我输入我的名字时,Gustavo 效果很好:
========== ==========
========== Gustavo ==========
========== ==========
但是当我输入我的全名时,它会在名字前添加很多空格:
========== ==========
========== Gustavo Marques ==========
========== ==========
我想要一个建议,这样我就可以使上下行的字符限制可变,或者只是找出程序在输入单词之前打印额外空格的原因 O_o。按照下面的代码操作:
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
coolConsolePrint cool = new coolConsolePrint();
Console.WriteLine("Write down what do you like to print:");
string thingToWrite = Console.ReadLine();
Console.Clear();
Console.WriteLine(cool.printWord(thingToWrite));
Console.ReadLine();
}
}
public class coolConsolePrint
{
public string printWord(String word)
{
int reps = 0;
int sets = 0;
string toPrintString = "";
int wordSize = word.Length;
char blank = ' ';
string style = "==========";
int styleSize = style.Length;
while (sets < 3)
{
if (reps == 2)
toPrintString += style.PadRight(wordSize, blank) + " " + word + " ";
else
{
if (reps % 2 != 0)
{
toPrintString += style + "\n".PadLeft(styleSize + wordSize + 2, blank);
sets++;
}
else
{
toPrintString += style.PadRight(styleSize + wordSize + 2, blank);
}
}
reps++;
}
return toPrintString;
}
}
}
你必须根据单词的长度来调整输出的宽度。这应该可以解决问题。
public string printWord(String word) {
string style = "==========";
int style_size = style.Length;
int content_size = word.Length;
int padding_size = 2; // makes sure that content does not sit right against style
int line_size = content_size + (style_size * 2) + (padding_size * 2);
string enclosing_line =
style.PadRight(line_size,'=')
+ Environment.NewLine;
string content_line =
style.PadRight(style_size + padding_size)
+ word
+ style.PadLeft((style_size + padding_size)
+ Environment.NewLine;
return enclosing_line + content_line + enclosing_line ;
}
编辑以修复语法错误
好吧,我正在复习 C# 并尝试制作一个简单的打印机。例如,您键入一个单词或短语并在其周围打印 = 符号。但是,调试它时,我注意到当我超过字符限制时,它会破坏样式。例如,当我输入我的名字时,Gustavo 效果很好:
========== ==========
========== Gustavo ==========
========== ==========
但是当我输入我的全名时,它会在名字前添加很多空格:
========== ==========
========== Gustavo Marques ==========
========== ==========
我想要一个建议,这样我就可以使上下行的字符限制可变,或者只是找出程序在输入单词之前打印额外空格的原因 O_o。按照下面的代码操作:
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
coolConsolePrint cool = new coolConsolePrint();
Console.WriteLine("Write down what do you like to print:");
string thingToWrite = Console.ReadLine();
Console.Clear();
Console.WriteLine(cool.printWord(thingToWrite));
Console.ReadLine();
}
}
public class coolConsolePrint
{
public string printWord(String word)
{
int reps = 0;
int sets = 0;
string toPrintString = "";
int wordSize = word.Length;
char blank = ' ';
string style = "==========";
int styleSize = style.Length;
while (sets < 3)
{
if (reps == 2)
toPrintString += style.PadRight(wordSize, blank) + " " + word + " ";
else
{
if (reps % 2 != 0)
{
toPrintString += style + "\n".PadLeft(styleSize + wordSize + 2, blank);
sets++;
}
else
{
toPrintString += style.PadRight(styleSize + wordSize + 2, blank);
}
}
reps++;
}
return toPrintString;
}
}
}
你必须根据单词的长度来调整输出的宽度。这应该可以解决问题。
public string printWord(String word) {
string style = "==========";
int style_size = style.Length;
int content_size = word.Length;
int padding_size = 2; // makes sure that content does not sit right against style
int line_size = content_size + (style_size * 2) + (padding_size * 2);
string enclosing_line =
style.PadRight(line_size,'=')
+ Environment.NewLine;
string content_line =
style.PadRight(style_size + padding_size)
+ word
+ style.PadLeft((style_size + padding_size)
+ Environment.NewLine;
return enclosing_line + content_line + enclosing_line ;
}
编辑以修复语法错误