C# Code
namespace PathSample
{
internal class Program
{
static void Main(string[] args)
{
List<string> target = new List<string>();
target.Add(@"\2024-10-1817.29.37.777.jpg"); // 正常路徑
target.Add(@"\2024-10-1817.29.37.777"); // 尾端沒有副檔名
target.Add(@"\2024-10-1817.29.37.777.jpg."); // 尾端有 . 符號
target.Add(@"\2024-10-1817.29.37.777.jpg\"); // 尾端有 \ 符號
string extension = string.Empty;
foreach (var path in target)
{
extension = Path.GetExtension(path);
extension = string.IsNullOrWhiteSpace(extension) ? "空值" : extension;
Console.WriteLine($"{path} 副檔名:{extension}");
}
}
}
}
顯示結果官方文件內容
文件備註
This method obtains the extension of path by searching path for a period (.), starting with the last character in path and continuing toward the first character. If a period is found before a DirectorySeparatorChar or AltDirectorySeparatorChar character, the returned string contains the period and the characters after it; otherwise, String.Empty is returned.C# SourceCode
基本上 GetExtension() 就是找 [最後的 \ 符號] 至尾端文字,該端文字內 [最後的 . 符號] 至尾端文字內容
沒有留言:
張貼留言