Browse Source

Add StripStart string extension method

pull/165/head
Ionite 1 year ago
parent
commit
e76ef3508e
No known key found for this signature in database
  1. 25
      StabilityMatrix.Core/Extensions/StringExtensions.cs

25
StabilityMatrix.Core/Extensions/StringExtensions.cs

@ -4,7 +4,8 @@ namespace StabilityMatrix.Core.Extensions;
public static class StringExtensions
{
private static string EncodeNonAsciiCharacters(string value) {
private static string EncodeNonAsciiCharacters(string value)
{
var sb = new StringBuilder();
foreach (var c in value)
{
@ -12,10 +13,11 @@ public static class StringExtensions
if (c > 127 || c < 32)
{
// This character is too big for ASCII
var encodedValue = "\\u" + ((int) c).ToString("x4");
var encodedValue = "\\u" + ((int)c).ToString("x4");
sb.Append(encodedValue);
}
else {
else
{
sb.Append(c);
}
}
@ -35,16 +37,18 @@ public static class StringExtensions
writer.Write("'");
foreach (var ch in str)
{
writer.Write(ch switch
writer.Write(
ch switch
{
'\0' => "\\0",
'\n' => "\\n",
'\r' => "\\r",
'\t' => "\\t",
// Non ascii
_ when ch > 127 || ch < 32 => $"\\u{(int) ch:x4}",
_ when ch > 127 || ch < 32 => $"\\u{(int)ch:x4}",
_ => ch.ToString()
});
}
);
}
writer.Write("'");
@ -71,4 +75,13 @@ public static class StringExtensions
}
return count;
}
/// <summary>
/// Strips the substring from the start of the string
/// </summary>
public static string StripStart(this string str, string subString)
{
var index = str.IndexOf(subString, StringComparison.Ordinal);
return index < 0 ? str : str.Remove(index, subString.Length);
}
}

Loading…
Cancel
Save