使用 Console.Write 字符串格式化
我目前正在编写我的示例代码,其中我想显示从服务器到客户端屏幕的数字数组。
基本上,我首先让服务器创建一个由 99 个随机生成的数字组成的数组,其值在 1 - 100 之间,将数组转换为字符串,然后使用字节发送将字符串传输到服务器。
代码如下所示:
//SERVER
int[] result = GenerateNumbers();
string resultingString = "";
for (int i = 0; i < result.Length; i++)
resultingString = resultingString + result[i] + ",";
s.Send(asen.GetBytes(resultingString));
//CLIENT
byte[] bb = new byte[1000];
int k = stm.Read(bb, 0, 1000);
for (int i = 0; i < k; i++)
{
Console.Write(Convert.ToChar(bb[i]));
}
现在我想做的是在客户端屏幕中显示结果数组。我的代码目前可以做到这一点。但是,使用 Console.Write()
命令,它会连续显示字符串直到结束。如下例所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 ... 93 94 95 96 97 98 99
我现在想做的是将显示格式设置为如下:
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 . . . . . . . . . .
90 91 92 93 94 95 96 97 98 99
有人可以给我指出一个好方法吗? :)
I'm currently working on this sample code of mine wherein I want to display an array of numbers coming from the server to the client screen.
Basically, I first make the server create an array of 99 randomly generated numbers who's values are from 1 - 100, convert the array into a string and then transport the string to the server using Byte sending.
The code looks like:
//SERVER
int[] result = GenerateNumbers();
string resultingString = "";
for (int i = 0; i < result.Length; i++)
resultingString = resultingString + result[i] + ",";
s.Send(asen.GetBytes(resultingString));
//CLIENT
byte[] bb = new byte[1000];
int k = stm.Read(bb, 0, 1000);
for (int i = 0; i < k; i++)
{
Console.Write(Convert.ToChar(bb[i]));
}
Now what I want to do is to show the resulting array in the client screen. My code currently can do that. However, with the Console.Write()
command, it continuously displays the string until it ends. As in the below example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 ... 93 94 95 96 97 98 99
What I want to do now is to make the display formatted like this:
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 . . . . . . . . . .
90 91 92 93 94 95 96 97 98 99
Can someone please point me to a good way to do this? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
String.PadLeft 就是你想要的 - 这将使用您选择的填充字符填充给定字符串的左侧,使其长度为 n 个字符。
要打破您需要的每个第 n 个数字(在 Console.Write(...); 之后):
String.PadLeft is what you're after - this'll pad the left hand side of a given string to make it n characters long, using the padding character of your choosing.
To break every nth number you need (after your Console.Write(...);):
我认为你的客户有问题。您正在发送一个逗号分隔的字符串,但我没有看到您处理它。
我以为使用的是UTF8。
我希望这有帮助:
I think there is a problem with your client. You are sending a comma separated string and I don't see you handling that.
I assumed that UTF8 was used.
I hope this helps:
使用 Console.WriteLine 方法 以适当的值换行。
Use Console.WriteLine Method to break to the new line on the appropriate value..