使用索引位置分割字符串(文件名)

Dom*_*zic 0 c# split lastindexof

我需要将文件名分成两个字符串的数组。

文件名如下所示:IMG-20190604-WA0005.jpg

我想要的数组:

[0] =“IMG-20190604-WA0005”

[1] =“jpg”

我使用索引位置LasIndexOf('.')

Tim*_*ter 6

不要使用字符串方法,而使用以下中的可用方法System.IO.Path

string file = "IMG-20190604-WA0005.jpg";
string filenameWithoutExtension = Path.GetFileNameWithoutExtension(file);
string extension = Path.GetExtension(file); 
Run Code Online (Sandbox Code Playgroud)

如果您不想要 . 一开始,将其删除:

string extension = Path.GetExtension(file).TrimStart('.'); 
Run Code Online (Sandbox Code Playgroud)