使用 Console.Write 字符串格式化

发布于 2024-11-09 04:33:47 字数 920 浏览 0 评论 0原文

我目前正在编写我的示例代码,其中我想显示从服务器到客户端屏幕的数字数组。

基本上,我首先让服务器创建一个由 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

睫毛上残留的泪 2024-11-16 04:33:47

String.PadLeft 就是你想要的 - 这将使用您选择的填充字符填充给定字符串的左侧,使其长度为 n 个字符。

要打破您需要的每个第 n 个数字(在 Console.Write(...); 之后):

if((i % n) == 0) {
  Console.WriteLine();
}

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

if((i % n) == 0) {
  Console.WriteLine();
}
稀香 2024-11-16 04:33:47

我认为你的客户有问题。您正在发送一个逗号分隔的字符串,但我没有看到您处理它。
我以为使用的是UTF8。
我希望这有帮助:

//CLIENT
//Makes an array to hold stream content
var bb = new byte[stm.Length];
//Fill the array with the stream content
stm.Read(bb, 0, (int)stm.Length);
//Converts the array of bytes back to a string
var rcvString = System.Text.UTF8Encoding.UTF8.GetString(bb);
//Split the string into an array using "," as separator
var array = rcvString.Split(new string[]{","}, 
    StringSplitOptions.RemoveEmptyEntries);
var pad = 2;   
var inc = 20;
var max = array.Length;
//Iterates through the array in "inc" intervals                        
for (var i = 0; i < max; i+=inc)
{
    //Iterates through a section of the array determined by "i" and "inc" 
    //(there is a special case if "inc" it's not a multiple of max)
    for (var j = 0; j < (max - i > inc ? inc : max - i); j += 1)
    {
        Console.Write(String.Format(@" {0}",array[i + j].PadLeft(pad)));
    }
    Console.WriteLine();
}

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:

//CLIENT
//Makes an array to hold stream content
var bb = new byte[stm.Length];
//Fill the array with the stream content
stm.Read(bb, 0, (int)stm.Length);
//Converts the array of bytes back to a string
var rcvString = System.Text.UTF8Encoding.UTF8.GetString(bb);
//Split the string into an array using "," as separator
var array = rcvString.Split(new string[]{","}, 
    StringSplitOptions.RemoveEmptyEntries);
var pad = 2;   
var inc = 20;
var max = array.Length;
//Iterates through the array in "inc" intervals                        
for (var i = 0; i < max; i+=inc)
{
    //Iterates through a section of the array determined by "i" and "inc" 
    //(there is a special case if "inc" it's not a multiple of max)
    for (var j = 0; j < (max - i > inc ? inc : max - i); j += 1)
    {
        Console.Write(String.Format(@" {0}",array[i + j].PadLeft(pad)));
    }
    Console.WriteLine();
}
得不到的就毁灭 2024-11-16 04:33:47

Use Console.WriteLine Method to break to the new line on the appropriate value..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文