1
0
Files
common/AMWD.Common/Extensions/StringExtensions.cs
2021-10-22 21:12:32 +02:00

122 lines
3.9 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace System
{
/// <summary>
/// String extensions.
/// </summary>
public static class StringExtensions
{
/// <summary>
/// Converts a hex string into a byte array.
/// </summary>
/// <param name="hexString">The hex encoded string.</param>
/// <param name="delimiter">A delimiter between the bytes (e.g. for MAC address).</param>
/// <returns>The bytes.</returns>
public static IEnumerable<byte> HexToBytes(this string hexString, string delimiter = "")
{
if (string.IsNullOrWhiteSpace(hexString))
yield break;
string str = string.IsNullOrEmpty(delimiter) ? hexString : hexString.Replace(delimiter, "");
if (str.Length % 2 == 1)
yield break;
for (int i = 0; i < str.Length; i += 2)
yield return Convert.ToByte(str.Substring(i, 2), 16);
}
/// <summary>
/// Converts a byte collection into a hex string.
/// </summary>
/// <param name="bytes">The bytes.</param>
/// <param name="delimiter">A delimiter to set between the bytes (e.g. for MAC address).</param>
/// <returns>The hex encoded string.</returns>
public static string BytesToHex(this IEnumerable<byte> bytes, string delimiter = "")
{
if (bytes?.Any() != true)
return null;
return string.Join(delimiter, bytes.Select(b => b.ToString("x2")));
}
/// <summary>
/// Encodes a string to the hexadecimal system (base 16).
/// </summary>
/// <param name="str"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static string HexEncode(this string str, Encoding encoding = null)
{
if (string.IsNullOrEmpty(str))
return str;
return (encoding ?? Encoding.Default).GetBytes(str).BytesToHex();
}
/// <summary>
/// Decodes a string from the hexadecimal system (base 16).
/// </summary>
/// <param name="str"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static string HexDecode(this string str, Encoding encoding = null)
{
if (string.IsNullOrEmpty(str))
return str;
return (encoding ?? Encoding.Default).GetString(str.HexToBytes().ToArray());
}
/// <summary>
/// Encodes a string to base64.
/// </summary>
/// <param name="str"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static string Base64Encode(this string str, Encoding encoding = null)
=> Convert.ToBase64String((encoding ?? Encoding.Default).GetBytes(str));
/// <summary>
/// Decodes a string from base64.
/// </summary>
/// <param name="str"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static string Base64Decode(this string str, Encoding encoding = null)
=> (encoding ?? Encoding.Default).GetString(Convert.FromBase64String(str));
/// <summary>
/// Replaces the search substring with the replacement when it was found at the beginning of the string.
/// </summary>
/// <param name="str">The string.</param>
/// <param name="search">The searched substring.</param>
/// <param name="replacement">The replacement.</param>
/// <returns></returns>
public static string ReplaceStart(this string str, string search, string replacement)
{
if (str.StartsWith(search))
return replacement + str.Substring(search.Length);
return str;
}
/// <summary>
/// Replaces the search substring with the replacement when it was found at the end of the string.
/// </summary>
/// <param name="str">The string.</param>
/// <param name="search">The searched substring.</param>
/// <param name="replacement">The replacement.</param>
/// <returns></returns>
public static string ReplaceEnd(this string str, string search, string replacement)
{
if (str.EndsWith(search))
return str.Substring(0, str.Length - search.Length) + replacement;
return str;
}
}
}