定义一个静态成员方法,该方法用于提取文件名

定义一个静态成员方法,该方法用于提取文件名。比如,给定一个字符串“c:\programfiles\Maths\all.dat”,使用该方法即可获取文件名all.dat。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "c:\\program files\\Maths\\all.dat";
            Console.WriteLine(getFilename(str));
            Console.ReadKey();
        }
        public static string getFilename(string file)
        {
            //提示:主体中使用string类的indexof方法和substring方法
            int i = file.LastIndexOf('\\');
            string str = file.Substring((++i));
            return str;
        }

    }
}


你可能感兴趣的:(定义一个静态成员方法,该方法用于提取文件名)