C#上机 第九周 任务1 用于提取文件名
2012-10-22 16:12
267 查看
/* * 程序头部注释开始 * 程序的版权和版本声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名称:用于提取文件名 * 作 者:薛广晨 * 完成日期:2012 年 10 月 22 日 * 版 本号:x1.0 * 对任务及求解方法的描述部分 * 输入描述: * 问题描述: 定义一个静态成员方法,该方法用于提取文件名。 * 比如,给定一个字符串“c:\program files\Maths\all.dat”,使用该方法即可获取文件名all.dat。 * 自行设计程序验证上述方法正确性。 public static string getFilename(string file) { //提示:主体中使用string类的indexof方法和substring方法 } * 程序输出: * 程序头部的注释结束 */ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { String file = @"c:\program files\Maths\all.dat"; String str = getFilename(file); Console.WriteLine("{0}的文件名是{1}", file, str); Console.ReadKey(); } //方法一 public static string getFilename(string file) { //提示:主体中使用string类的indexof方法和substring方法 int index = 0; int num = 0; while ((index = file.IndexOf(@"\", index)) != -1) { index += 1; if (index != -1) { num = index; } } return file.Substring(num); } //方式二 /*public static string getFilename(string file) { //提示:主体中使用string类的indexof方法和substring方法 int index = 0; string str = file; while ((index = str.IndexOf(@"\")) != -1) { str = str.Substring(index + 1); } return str; }*/ } }
运行结果:
相关文章推荐
- C#--第九周实验--任务1--定义一个静态成员方法,该方法用于提取文件名。
- C#--第九周 任务1--定义一个静态成员方法,该方法用于提取文件名
- C#上机 第九周 任务四 计算数列的极限值
- C#上机 第九周 任务2 实现字符串反转
- C#上机 第九周 任务2 从题库中随机抽题
- 第九周上机任务(二)
- 第九周上机任务2
- 第九周上机任务项目1:分段函数求值
- 详解C# 不能用于文件名的字符
- 第九周上机任务3
- C#上机 第七周 任务1 继承
- 个人所得税计算器(第九周上机任务)
- 利息计算器(第九周上机任务3)。
- 第九周上机任务项目2-Time类
- C#提取文件名
- 日历表(第九周上机任务)。(不用掰手指头算了吧)
- 第九周上机任务--利息计算器
- 第九周上机任务四--本月有几天
- C#添加系统计划任务,用于自启动应用
- C#(asp.net)多线程用法示例(可用于同时处理多个任务)