C#提取文件名
題目描述
假設有一個字符串包含了文件名、擴展名和路徑,如strFileName=@“D:\C#程序設計\實驗3\MyFile.TXT”。請使用C#編寫一個靜態方法,該方法能夠取出路徑中的文件名“MyFile.TXT”。
輸入
一個包含了文件名,擴展名和路徑的字符串。
輸出
字符串中的文件名。
樣例輸入
strFileName=@“D:\C#程序設計\實驗3\MyFile.TXT”
樣例輸出
MyFile.TXT
提示
提示:使用string類的lastindexof和substring等方法實現。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApplication1
{class Program{static void Main(string[] args){string str = Console.ReadLine();int str1 = str.LastIndexOf("\\");int str2 = str.LastIndexOf("TXT");string str3 = str.Substring(str1 + 1,str2+2-str1);Console.WriteLine(str3);}}
}
?