diff --git a/Repository/Extension/StringExtensions.cs b/Repository/Extension/StringExtensions.cs new file mode 100644 index 0000000..bf607bb --- /dev/null +++ b/Repository/Extension/StringExtensions.cs @@ -0,0 +1,57 @@ +using System; +using System.Text.RegularExpressions; + +namespace Repository.Extension +{ + public static class StringExtensions + { + /// + /// 原字符轉換Niagara特殊字符 + /// + /// 原始字串 + /// 轉換後的字串 + public static string ConvertN4(this string input) + { + if (string.IsNullOrEmpty(input)) return input; + + // 數字開頭加上 "$3" + if (Regex.IsMatch(input, @"^\d")) + { + input = "$3" + input; + } + + // 包含 "-" 轉換為 "$2d" + if (input.Contains("-")) + { + input = input.Replace("-", "$2d"); + } + + return input; + } + /// + /// Niagara特殊字符轉換原字符 + /// + /// 原始字串 + /// 轉換後的字串 + public static string ConvertNormal(this string input) + { + if (string.IsNullOrEmpty(input)) return input; + + // 包含"$2d" 轉換回 "-" + if (input.Contains("$2d")) + { + input = input.Replace("$2d", "-"); + } + + // "$3" 開頭移除 "$3" + if (input.StartsWith("$3")) + { + input = input.Substring(2); // 移除前兩個字元 + } + + return input; + } + + } + +}