[通用]N4特殊字串轉換方法

This commit is contained in:
張家睿 2024-12-24 15:27:07 +08:00
parent d41217adbd
commit 57ea689560

View File

@ -0,0 +1,57 @@
using System;
using System.Text.RegularExpressions;
namespace Repository.Extension
{
public static class StringExtensions
{
/// <summary>
/// 原字符轉換Niagara特殊字符
/// </summary>
/// <param name="input">原始字串</param>
/// <returns>轉換後的字串</returns>
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;
}
/// <summary>
/// Niagara特殊字符轉換原字符
/// </summary>
/// <param name="input">原始字串</param>
/// <returns>轉換後的字串</returns>
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;
}
}
}