From 5c447f1b0266dbfff712879c42ffa96c9d246223 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Mon, 20 Apr 2020 14:01:48 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20string.ChangeEncoding?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Converts this string from one encoding to another, by using the previously defined .GetBytes and .GetString as intermediary methods --- X10D/src/StringExtensions.cs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/X10D/src/StringExtensions.cs b/X10D/src/StringExtensions.cs index e80f2c4..9b1c1fd 100644 --- a/X10D/src/StringExtensions.cs +++ b/X10D/src/StringExtensions.cs @@ -32,6 +32,39 @@ return Convert.ToBase64String(value.GetBytes()); } + /// + /// Converts this string from one encoding to another. + /// + /// The input string. + /// The input encoding. + /// The output encoding. + /// Returns a new with its data converted to + /// . + /// is + /// - or - + /// is + /// -or + /// is . + 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); + } + /// /// Parses a into an . ///