-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomStringExtensions.cs
90 lines (88 loc) · 3.18 KB
/
CustomStringExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
namespace NetFrameworkHelper
{
/// <summary>
/// This class contains custom extensions for use with strings
/// </summary>
public static class CustomStringExtensions
{
/// <summary>
/// Returns a string literal from a specified number of characters from the right of a string
/// </summary>
/// <param name="str">sting this method is called against</param>
/// <param name="start">Number of characters to return from the right side of a string</param>
/// <returns>String</returns>
public static string Right(this string str, int start)
{
if (string.IsNullOrEmpty(str))
{
throw new ArgumentNullException(nameof(str), "String cannot be null or empty");
}
if (start > str.Length)
{
throw new ArgumentOutOfRangeException(nameof(str), "Number of characters greater than the string length");
}
return str.Substring(str.Length - start, start);
}
/// <summary>
/// Returns a string literal from a specified number of characters from the start of a string
/// </summary>
/// <param name="str">sting this method is called against</param>
/// <param name="nCount">Number of characters to return from the left</param>
/// <returns>String</returns>
public static string Left(this string str, int nCount)
{
if (string.IsNullOrEmpty(str))
{
throw new ArgumentNullException(nameof(str), "String cannot be null or empty");
}
if (nCount > str.Length)
{
throw new ArgumentOutOfRangeException(nameof(str), "Number of characters greater than the string length");
}
return str.Substring(0, nCount);
}
/// <summary>
/// Returns a string from a specified start position
/// </summary>
/// <param name="str">sting this method is called against</param>
/// <param name="start">Character number to start with</param>
/// <returns>String</returns>
public static string Mid(this string str, int start)
{
if (string.IsNullOrEmpty(str))
{
throw new ArgumentNullException(nameof(str), "String cannot be null or empty");
}
if (start > str.Length)
{
throw new ArgumentOutOfRangeException(nameof(str), "Number of characters greater than the string length");
}
return str.Substring(start);
}
/// <summary>
/// Returns a string from a specified start position
/// </summary>
/// <param name="str">sting this method is called against</param>
/// <param name="start">Character number to start with</param>
/// <param name="stop">Character number to stop with</param>
/// <returns>String</returns>
public static string Mid(this string str, int start, int stop)
{
if (string.IsNullOrEmpty(str))
{
throw new ArgumentNullException(nameof(str), "String cannot be null or empty");
}
if (start > str.Length)
{
throw new ArgumentOutOfRangeException(nameof(str), "Number of characters greater than the string length");
}
//Check if start is less than 0
start = (start < 0) ? 0 : start;
//check if the requested stop position is longer than the returnes tring would be minus the start position
//if it is, then set stop to the string length minus the start position
stop = (stop > (str.Length - start)) ? str.Length - start : stop;
return str.Substring(start, stop);
}
}
}