aboutsummaryrefslogtreecommitdiffstats
path: root/src/IOL.Helpers/StringHelpers.cs
diff options
context:
space:
mode:
authorivarlovlie <git@ivarlovlie.no>2021-04-01 22:36:59 +0200
committerivarlovlie <git@ivarlovlie.no>2021-04-01 22:36:59 +0200
commit9e0fbac6d3cb4e11efdb33c339ccf0adbca0bc6c (patch)
tree8fd1e8710abc3b17f0bc7f960c89931efd83c944 /src/IOL.Helpers/StringHelpers.cs
downloaddotnet-helpers-9e0fbac6d3cb4e11efdb33c339ccf0adbca0bc6c.tar.xz
dotnet-helpers-9e0fbac6d3cb4e11efdb33c339ccf0adbca0bc6c.zip
Initial commit
Diffstat (limited to 'src/IOL.Helpers/StringHelpers.cs')
-rw-r--r--src/IOL.Helpers/StringHelpers.cs67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/IOL.Helpers/StringHelpers.cs b/src/IOL.Helpers/StringHelpers.cs
new file mode 100644
index 0000000..1dde161
--- /dev/null
+++ b/src/IOL.Helpers/StringHelpers.cs
@@ -0,0 +1,67 @@
+using System;
+using System.Text;
+using System.Text.RegularExpressions;
+
+namespace IOL.Helpers
+{
+ public static class StringHelpers
+ {
+ public static bool IsNullOrWhiteSpace(this string value) {
+ return string.IsNullOrWhiteSpace(value);
+ }
+
+ public static string Slugified(this string input) {
+ return Slug.Generate(true, input);
+ }
+
+ public static bool HasValue(this string value) {
+ return !value.IsNullOrWhiteSpace();
+ }
+
+ public static Guid ToGuid(this string value) {
+ return Guid.Parse(value);
+ }
+
+ public static string Base64Encode(this string text) {
+ return Convert.ToBase64String(Encoding.UTF8.GetBytes(text));
+ }
+
+ public static string ExtractFileName(this string value) {
+ if (value.IsNullOrWhiteSpace()) return default;
+ var lastIndex = value.LastIndexOf('.');
+ return lastIndex <= 0 ? default : value.Substring(0, lastIndex);
+ }
+
+ public static string ExtractExtension(this string value) {
+ if (value.IsNullOrWhiteSpace()) return default;
+ var lastIndex = value.LastIndexOf('.');
+ return lastIndex <= 0 ? default : value.Substring(lastIndex);
+ }
+
+ public static string Capitalize(this string input) {
+ return input.IsNullOrWhiteSpace()
+ ? input
+ : Regex.Replace(input, @"\b(\w)", m => m.Value.ToUpper(), RegexOptions.None);
+ }
+
+
+ /// <summary>
+ /// Check if the given MIME is a JSON MIME.
+ /// </summary>
+ /// <param name="mime">MIME</param>
+ /// <returns>Returns true if MIME type is json.</returns>
+ public static bool IsJsonMime(this string mime) {
+ var jsonRegex = new Regex("(?i)^(application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(;.*)?$");
+ return mime != null && (jsonRegex.IsMatch(mime) || mime.Equals("application/json-patch+json"));
+ }
+
+ public static string Obfuscate(this string value) {
+ var last4Chars = "****";
+ if (value.HasValue() && value.Length > 4) {
+ last4Chars = value.Substring(value.Length - 4);
+ }
+
+ return "****" + last4Chars;
+ }
+ }
+} \ No newline at end of file