From 57ea6895608d144c5e3f04dfbb95fc38b82d692d Mon Sep 17 00:00:00 2001 From: "jay.chang" Date: Tue, 24 Dec 2024 15:27:07 +0800 Subject: [PATCH] =?UTF-8?q?[=E9=80=9A=E7=94=A8]N4=E7=89=B9=E6=AE=8A?= =?UTF-8?q?=E5=AD=97=E4=B8=B2=E8=BD=89=E6=8F=9B=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Repository/Extension/StringExtensions.cs | 57 ++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Repository/Extension/StringExtensions.cs 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; + } + + } + +}