C # obtain the file name in Task 1 of week 9 on the machine

Source: Internet
Author: User

[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:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.