/
StringExtensions.cs
105 lines (91 loc) · 3.66 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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using FluentAssertions.Formatting;
namespace FluentAssertions.Common
{
internal static class StringExtensions
{
/// <summary>
/// Finds the first index at which the <paramref name="value"/> does not match the <paramref name="expected"/>
/// string anymore, accounting for the specified <paramref name="stringComparison"/>.
/// </summary>
public static int IndexOfFirstMismatch(this string value, string expected, StringComparison stringComparison)
{
for (int index = 0; index < value.Length; index++)
{
if ((index >= expected.Length) || !value[index].ToString().Equals(expected[index].ToString(), stringComparison))
{
return index;
}
}
return -1;
}
/// <summary>
/// Gets the quoted three characters at the specified index of a string, including the index itself.
/// </summary>
public static string IndexedSegmentAt(this string value, int index)
{
int length = Math.Min(value.Length - index, 3);
string formattedString = Formatter.ToString(value.Substring(index, length));
return $"{formattedString} (index {index})".Replace("{", "{{").Replace("}", "}}");
}
/// <summary>
/// Replaces all characters that might conflict with formatting placeholders and newlines with their escaped counterparts.
/// </summary>
public static string Escape(this string value, bool escapePlaceholders = false)
{
value = value.Replace("\"", "\\\"").Replace("\n", @"\n").Replace("\r", @"\r");
if (escapePlaceholders)
{
value = value.Replace("{", "{{").Replace("}", "}}");
}
return value;
}
/// <summary>
/// Replaces all characters that might conflict with formatting placeholders and newlines with their escaped counterparts.
/// </summary>
internal static string Unescape(this string value, bool escapePlaceholders = false)
{
value = value.Replace("\\\"", "\"").Replace(@"\n", "\n").Replace(@"\r", "\r");
if (escapePlaceholders)
{
value = value.Replace("{{", "{").Replace("}}", "}");
}
return value;
}
public static bool IsNullOrEmpty(this string value)
{
return string.IsNullOrEmpty(value);
}
/// <summary>
/// Joins a string with one or more other strings using a specified separator.
/// </summary>
/// <remarks>
/// Any string that is empty (including the original string) is ignored.
/// </remarks>
public static string Combine(this string @this, string other, string separator = ".")
{
if (@this.Length == 0)
{
return (other.Length != 0) ? other : string.Empty;
}
if (other.Length == 0)
{
return (@this.Length != 0) ? @this : string.Empty;
}
return @this + separator + other;
}
/// <summary>
/// Changes the first character of a string to uppercase.
/// </summary>
public static string Capitalize(this string @this)
{
char[] charArray = @this.ToCharArray();
charArray[0] = char.ToUpper(charArray[0]);
return new string(charArray);
}
public static string RemoveNewLines(this string @this)
{
return @this.Replace("\n", "").Replace("\r", "").Replace("\\r\\n", "");
}
}
}