-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathExtensions.cs
42 lines (39 loc) · 1.25 KB
/
Extensions.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Calculator
{
/// <summary>
/// Collection of extension functions
/// </summary>
public static class Extensions
{
/// <summary>
/// Reverse the string
/// </summary>
/// <param name="s">this string object</param>
/// <returns>The string reversed</returns>
public static string Reverse(this string s)
{
char[] charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
/// <summary>
/// Split a string every nth character
/// </summary>
/// <param name="s">this string object</param>
/// <param name="n">number of characters per chunk</param>
/// <returns></returns>
public static IEnumerable<String> SplitEveryNth(this String s, Int32 n)
{
if (s == null)
throw new ArgumentNullException("s");
if (n <= 0)
throw new ArgumentException("N has to be positive.", "partLength");
for (var i = 0; i < s.Length; i += n)
yield return s.Substring(i, Math.Min(n, s.Length - i));
}
}
}