diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2021-04-23 23:10:26 +0200 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2021-04-23 23:10:26 +0200 |
| commit | 8cbf8cbc9f450db8f3422cae610289effc6d1c1f (patch) | |
| tree | 085f982915091ca9af44f273505be617da5ea342 /src/IOL.Helpers/StringHelpers.cs | |
| parent | f227d1c15e619eb3d7b44e1475a223513489d2d3 (diff) | |
| download | dotnet-helpers-8cbf8cbc9f450db8f3422cae610289effc6d1c1f.tar.xz dotnet-helpers-8cbf8cbc9f450db8f3422cae610289effc6d1c1f.zip | |
Add UnicornFormat
Diffstat (limited to 'src/IOL.Helpers/StringHelpers.cs')
| -rw-r--r-- | src/IOL.Helpers/StringHelpers.cs | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/src/IOL.Helpers/StringHelpers.cs b/src/IOL.Helpers/StringHelpers.cs index 1dde161..c090117 100644 --- a/src/IOL.Helpers/StringHelpers.cs +++ b/src/IOL.Helpers/StringHelpers.cs @@ -1,6 +1,9 @@ using System; +using System.Collections.Generic; +using System.Linq; using System.Text; using System.Text.RegularExpressions; +using Microsoft.Extensions.Configuration; namespace IOL.Helpers { @@ -18,6 +21,23 @@ namespace IOL.Helpers return !value.IsNullOrWhiteSpace(); } + public static string UnicornFormat(this string input, IDictionary<string, string> values) { + if (string.IsNullOrWhiteSpace(input)) return default; + return values.Count == 0 ? default : values.Aggregate(input, (current, value1) => current.Replace("{" + value1.Key + "}", value1.Value)); + } + + + public static string UnicornFormatWithEnvironment(this string input, IConfiguration configuration) { + if (string.IsNullOrWhiteSpace(input)) return default; + var matchList = Regex.Matches(input, ""); + foreach (var key in matchList.Select(match => match.Value)) { + var value = configuration.GetValue<string>(key); + input = input.Replace(key, value); + } + + return input; + } + public static Guid ToGuid(this string value) { return Guid.Parse(value); } @@ -29,7 +49,7 @@ namespace IOL.Helpers public static string ExtractFileName(this string value) { if (value.IsNullOrWhiteSpace()) return default; var lastIndex = value.LastIndexOf('.'); - return lastIndex <= 0 ? default : value.Substring(0, lastIndex); + return lastIndex <= 0 ? default : value[..lastIndex]; } public static string ExtractExtension(this string value) { @@ -40,8 +60,8 @@ namespace IOL.Helpers public static string Capitalize(this string input) { return input.IsNullOrWhiteSpace() - ? input - : Regex.Replace(input, @"\b(\w)", m => m.Value.ToUpper(), RegexOptions.None); + ? input + : Regex.Replace(input, @"\b(\w)", m => m.Value.ToUpper(), RegexOptions.None); } @@ -64,4 +84,4 @@ namespace IOL.Helpers return "****" + last4Chars; } } -}
\ No newline at end of file +} |
