C#
29.C# - String Class의 중요 메서드
Godffs
2009. 8. 7. 18:34
반응형
전체 경로가 입력되었을 때 파일명과 확장자 추출 예제입니다.
using System;
public class 파일명추출
{
//전체 경로가 입력되었을 때 파일명과 확장자 추출
public static void Main(string[] args)
{
string dir = "c:\\Website\\redPlus\\Images\\test.gif";
string fullName = String.Empty;
string name = "";
string ext = name;
//전체 파일명 : test.gif
fullName = dir.Substring(dir.LastIndexOf('\\') + 1);
//순수 파일명 : test
name = fullName.Substring(0, fullName.LastIndexOf('.'));
//확장자 : gif
ext = fullName.Substring(fullName.LastIndexOf('.'));
Console.WriteLine(fullName);
Console.WriteLine((name));
Console.WriteLine(ext);
}
}
public class 파일명추출
{
//전체 경로가 입력되었을 때 파일명과 확장자 추출
public static void Main(string[] args)
{
string dir = "c:\\Website\\redPlus\\Images\\test.gif";
string fullName = String.Empty;
string name = "";
string ext = name;
//전체 파일명 : test.gif
fullName = dir.Substring(dir.LastIndexOf('\\') + 1);
//순수 파일명 : test
name = fullName.Substring(0, fullName.LastIndexOf('.'));
//확장자 : gif
ext = fullName.Substring(fullName.LastIndexOf('.'));
Console.WriteLine(fullName);
Console.WriteLine((name));
Console.WriteLine(ext);
}
}
반응형