Skip to content

Instantly share code, notes, and snippets.

@ttkoma
Created September 29, 2022 14:23
Show Gist options
  • Save ttkoma/7fa1430dd302b569595d42e607648353 to your computer and use it in GitHub Desktop.
Save ttkoma/7fa1430dd302b569595d42e607648353 to your computer and use it in GitHub Desktop.
TripleEncodingChars
public static string TripleEncodeSymbol(this string input, string targetSymbol, string firstSymbol = "$", string secondSymbol = "Z")
{
if (targetSymbol == firstSymbol)
throw new ArgumentOutOfRangeException($"{nameof(targetSymbol)} can't be equal {nameof(firstSymbol)}");
if (targetSymbol == secondSymbol)
throw new ArgumentOutOfRangeException($"{nameof(targetSymbol)} can't be equal {nameof(secondSymbol)}");
//return input.Replace("$", "$$$").Replace("Z", "$$Z").Replace(":", "$ZZ");
return input
.Replace(firstSymbol, firstSymbol + firstSymbol + firstSymbol)
.Replace(secondSymbol, firstSymbol + firstSymbol + secondSymbol)
.Replace(targetSymbol, firstSymbol + secondSymbol + secondSymbol);
}
public static string TripleDecodeSymbol(this string input, string targetSymbol, string firstSymbol = "$", string secondSymbol = "Z")
{
if (targetSymbol == firstSymbol)
throw new ArgumentOutOfRangeException($"{nameof(targetSymbol)} can't be equal {nameof(firstSymbol)}");
if (targetSymbol == secondSymbol)
throw new ArgumentOutOfRangeException($"{nameof(targetSymbol)} can't be equal {nameof(secondSymbol)}");
//return input.Replace("$ZZ", ":").Replace("$$Z", "Z").Replace("$$$", "$");
return input
.Replace(firstSymbol + secondSymbol + secondSymbol, targetSymbol)
.Replace(firstSymbol + firstSymbol + secondSymbol, secondSymbol)
.Replace(firstSymbol + firstSymbol + firstSymbol, firstSymbol);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment