[通用]N4特殊字串轉換方法
This commit is contained in:
parent
d41217adbd
commit
57ea689560
57
Repository/Extension/StringExtensions.cs
Normal file
57
Repository/Extension/StringExtensions.cs
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user