Python print "0" * 5 equivalent in C #

When I need to print "00000", I can use "0" * 5 in python. Is there an equivalent in C # without a loop?

+5
source share
7 answers

According to your example, I suggest that you will use these lines to help reset some numbers. In this case, it would be easier to use the method String.PadLeft()to complete your addition. You can also use a similar function for python, rjust().

eg.

var str = "5";
var padded = str.PadLeft(8, '0'); // pad the string to 8 characters, filling in '0's
// padded = "00000005"

, , String.Concat() Enumerable.Repeat(). .

.

var chr = '0';
var repeatedChr = new String(chr, 8);
// repeatedChr = "00000000";
var str = "ha";
// var repeatedStr = new String(str, 5); // error, no equivalent
var repeated = String.Concat(Enumerable.Repeat(str, 5));
// repeated = "hahahahaha"
+5

String ctor overloads :

string zeros = new String('0', 5);
+5

, , string s = new string("O", 5);. .

Enumerable.Repeat() using System.Linq; .

string s = string.Concat(Enumerable.Repeat("O", 5));
+4

 string s = new string( '0', 5 );

+2

:

int n = 0;
string s = n.ToString().PadRight(5, '0');
0

: string.Join("0", new string[6]);

"00000"?!

0

This one only works with zero: 0.ToString("D5");

0
source

All Articles