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? 🙂
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(…);):