LeftPad in C#
We'll cover the following...
Press + to interact
using System;using System.Text;public class LeftPad{public static String leftPad(String str,int paddedLength,char ch = '.') {if (paddedLength <= str.Length) {return str;}StringBuilder sb = new StringBuilder();paddedLength = paddedLength - str.Length;for (int i = 0; i < paddedLength; ++i) {sb.Append(ch);}sb.Append(str);return sb.ToString();}public static void Main() {Console.WriteLine(leftPad("1", 1));Console.WriteLine(leftPad("2", 2));Console.WriteLine(leftPad("3", 3));Console.WriteLine(leftPad("4", 4));Console.WriteLine(leftPad("5", 5));Console.WriteLine(leftPad("hello", 7));Console.WriteLine(leftPad("foo", 6));Console.WriteLine(leftPad("foo", 3));Console.WriteLine(leftPad("foobar", 3));Console.WriteLine(leftPad("foo", 6, '?'));}}
to save progress
LeftPad in Perl
LeftPad in Haskell
to save progress