How to add N zeros to an empty string in C#

2 Answers

0 votes
using System;

public class Program
{
	public static string addToEmptyStr(string pad, int len) {
		string str = "";

		while (str.Length < len) {
			str += pad;
		}

		return str;
	}

	public static void Main(string[] args)
	{
		int n = 4;

		string empty_string = addToEmptyStr("0", n);

		Console.WriteLine(empty_string);
	}
}



/*
run
 
0000
 
*/

 



answered May 26, 2024 by avibootz
0 votes
using System;
 
public class Program
{
    public static void Main(string[] args)
    {
        int n = 4;
        string empty_string = "";
 
        empty_string = empty_string.PadLeft(n, '0');
 
        Console.WriteLine(empty_string);
    }
}
 
 
 
/*
run
  
0000
  
*/

 



answered May 26, 2024 by avibootz

Related questions

1 answer 120 views
1 answer 128 views
2 answers 144 views
2 answers 133 views
1 answer 117 views
117 views asked May 26, 2024 by avibootz
1 answer 135 views
2 answers 153 views
...