40
1
using System;
2
public static class Program
3
{
4
public static void Main()
5
{
6
string dq = "\"";
7
string numbers = "12345";
8
9
Console.WriteLine( dq + numbers.TrimStart(1) + dq );
10
Console.WriteLine( dq + numbers.TrimStart(50) + dq );
11
Console.WriteLine( dq + numbers.TrimEnd(1) + dq );
12
Console.WriteLine( dq + numbers.TrimEnd(50) + dq );
13
Console.WriteLine( dq + numbers.Truncate(1) + dq );
14
Console.WriteLine( dq + numbers.Truncate(50) + dq );
15
16
}
17
18
public static string TrimStart(this string input, int length)
19
{
20
if (string.IsNullOrEmpty(input) || length < 0) return input;
21
22
if (length > input.Length) return input;
23
24
return input.Substring(length);
25
}
26
public static string TrimEnd(this string input, int length)
27
{
28
if (string.IsNullOrEmpty(input) || length < 1) return input;
29
30
if (length > input.Length) return input;
31
32
return input.Substring(0, input.Length - length);
33
}
34
35
public static string Truncate(this string value, int maxLength)
36
{
37
if (string.IsNullOrEmpty(value)) return value;
38
return value.Length <= maxLength ? string.Empty : value.Substring(0, maxLength);
39
}
40
}
Cached Result