1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-23 00:58:48 +00:00

Add string.ChangeEncoding

Converts this string from one encoding to another, by using the
previously defined .GetBytes and .GetString as intermediary methods
This commit is contained in:
Oliver Booth 2020-04-20 14:01:48 +01:00
parent 27e82bada1
commit 5c447f1b02
No known key found for this signature in database
GPG Key ID: 0D7F2EF1C8D2B9C0

View File

@ -32,6 +32,39 @@
return Convert.ToBase64String(value.GetBytes()); return Convert.ToBase64String(value.GetBytes());
} }
/// <summary>
/// Converts this string from one encoding to another.
/// </summary>
/// <param name="str">The input string.</param>
/// <param name="from">The input encoding.</param>
/// <param name="to">The output encoding.</param>
/// <returns>Returns a new <see cref="string"/> with its data converted to
/// <paramref name="to"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="str"/> is <see langword="null"/>
/// - or -
/// <paramref name="from"/> is <see langword="null"/>
/// -or
/// <paramref name="to"/> is <see langword="null"/>.</exception>
public static string ChangeEncoding(this string str, Encoding from, Encoding to)
{
if (str is null)
{
throw new ArgumentNullException(nameof(str));
}
if (from is null)
{
throw new ArgumentNullException(nameof(from));
}
if (to is null)
{
throw new ArgumentNullException(nameof(to));
}
return str.GetBytes(from).GetString(to);
}
/// <summary> /// <summary>
/// Parses a <see cref="string"/> into an <see cref="Enum"/>. /// Parses a <see cref="string"/> into an <see cref="Enum"/>.
/// </summary> /// </summary>