[Fork] TrimStartTrimEndwithInteger

CSharp · Console

/Program.cs

using System;					
public static class Program
{	
	public static void Main()
	{
		string dq = "\""; 
		string numbers = "12345";
		
		Console.WriteLine(	dq + numbers.TrimStart(1) 	+ dq ); 
		Console.WriteLine(	dq + numbers.TrimStart(50) 	+ dq ); 
		Console.WriteLine(	dq + numbers.TrimEnd(1)		+ dq ); 
		Console.WriteLine(	dq + numbers.TrimEnd(50) 	+ dq ); 
		Console.WriteLine(	dq + numbers.Truncate(1) 	+ dq ); 
		Console.WriteLine(	dq + numbers.Truncate(50) 	+ dq ); 
		
	}
	
	public static string TrimStart(this string input, int length)
	{
		if (string.IsNullOrEmpty(input) || length < 0) return input;
		
		if (length > input.Length) return input;
		
		return input.Substring(length); 
	}
	public static string TrimEnd(this string input, int length)
	{
		if (string.IsNullOrEmpty(input) || length < 1) return input;
		
		if (length > input.Length) return input;
		
		return input.Substring(0, input.Length - length);
	}

	public static string Truncate(this string value, int maxLength)
    {
        if (string.IsNullOrEmpty(value)) return value;
        return value.Length <= maxLength ? string.Empty : value.Substring(0, maxLength); 
    }
}
;