-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathStringExtensions.cs
33 lines (32 loc) · 1.05 KB
/
StringExtensions.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
using System.Text;
namespace UEx
{
public static class StringExtensions
{
/// <summary>
/// Insert the specified character into the string every n characters
/// </summary>
/// <param name="input"></param>
/// <param name="insertCharacter"></param>
/// <param name="n"></param>
/// <param name="charsInserted"></param>
/// <returns></returns>
public static string InsertCharEveryNChars(this string input, char insertCharacter, int n, out int charsInserted)
{
charsInserted = 0;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < input.Length; i++)
{
if (i % n == 0)
{
sb.Append(insertCharacter);
++charsInserted;
}
if (input[i] == insertCharacter)
++charsInserted;
sb.Append(input[i]);
}
return sb.ToString();
}
}
}