[Csharp]
/*
* Start the program header annotation.
* Copyright and version Declaration of the program
* Copyright (c) 2011, a student from the computer College of Yantai University
* All rights reserved.
* File name: used to extract the file name
* Author: Xue Guangchen
* Completion date: January 1, October 22, 2012
* Version No.: x1.0
* Description of tasks and Solutions
* Input description:
* Problem description: defines a static member method used to extract file names.
* For example, if you specify a string "c: \ program files \ Maths \ all. dat", you can use this method to obtain the file name all. dat.
* Independently design a program to verify the correctness of the above method.
Public static string getFilename (string file)
{
// Prompt: Use the indexof method and substring method of the string class in the subject
}
* Program output:
* End the comment in the program Header
*/
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} file name is {1}", file, str );
Console. ReadKey ();
}
// Method 1
Public static string getFilename (string file)
{
// Prompt: Use the indexof method and substring method of the string class in the subject
Int index = 0;
Int num = 0;
While (index = file. IndexOf (@ "\", index ))! =-1)
{
Index + = 1;
If (index! =-1)
{
Num = index;
}
}
Return file. Substring (num );
}
// Method 2
/* Public static string getFilename (string file)
{
// Prompt: Use the indexof method and substring method of the string class in the subject
Int index = 0;
String str = file;
While (index = str. IndexOf (@"\"))! =-1)
{
Str = str. Substring (index + 1 );
}
Return str;
}*/
}
}
Running result: