58 lines
1.5 KiB
C#
58 lines
1.5 KiB
C#
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;
|
|
}
|
|
|
|
}
|
|
|
|
}
|