어떻게 어떻게 할 만 information.xml 필요문자열에서 문자를 제거하는 방법은 무엇입니까?
string s="testpage\information.xml"
.. 문자열의 일부 문자를 제거하려면?
어떻게 어떻게 할 만 information.xml 필요문자열에서 문자를 제거하는 방법은 무엇입니까?
string s="testpage\information.xml"
.. 문자열의 일부 문자를 제거하려면?
System.IO.Path
을 추출하기 위해 Path
클래스를 사용합니다. 귀하의 경우에는 Path.GetFileName(string path)
을 사용하여 문자열에서 파일 이름을 가져올 수 있습니다.
예
string s = @"testpage\information.xml";
string filename = Path.GetFileName(s);
//MessageBox.Show(filename);
감사합니다,
난 당신이 도움이 :)
항상 파일 경로를 s
에있을 것입니다 값입니다 가정하면, 문자열이 파일 경로 정보를 포함하고 있기 때문에이 당신을 도울 수있는 파일 이름
var filename = Path.GetFileName(s);
파일 경로가
aaaa\bbb\ccc\dddd\information.xml
마지막 문자열을 검색하려면 양식입니다되기를 바랍니다 , 구분 기호 0123을 사용하여 문자열을 나눌 수 있습니다 그것은 여부를 확인하지 않습니다
String path = @"aaaa\bbb\ccc\dddd\information.xml";
String a[] = path.Split('\\');
이렇게하면 파일 경로를 사용하는 경우 당신은
String filename = a[a.Length-1];
로 파일 이름을 검색 할 수 있습니다 ["aaaa", "bbb", "ccc", "dddd", "information.xml"]
로 문자열 배열을 줄 것이라고 Path.GetFileName 방법을 참조 파일이 존재하는지 여부. 그래서 더 빠를 것입니다.
s = Path.GetFileName(s);
파일이 있는지 여부를 확인하려면 File.Exists 클래스를 사용하십시오.
또 다른 방법은 사항 String.split() 메소드를
string[] arr = s.Split('\\');
if(arr.Length > 0)
{
s = arr[arr.Length - 1];
}
또 다른 방법은 파일 경로를 될 것입니다 경우 정규식
s = Regex.Match(s, @"[^\\]*$").Value;
그 다음 사용할 수있는 백 슬래시 구분 항상 맞다 경우 : C에서
if (s.Contains(@"\"))
s= s.Substring(s.IndexOf(@"\") + 1);
것은이 같은 작업을 수행 할 수 있습니다 ++.기본적으로 경로의 오른쪽에서 왼쪽으로 "/"또는 "\"를 검색 및 구분 기호의 첫 번째 occurance에서 시작하는 문자열 자르기 :이
string ExtractFileName(const string& strPathFileName)
{
size_t npos;
string strOutput = strPathFileName;
if(strPathFileName.rfind(L'/', npos) || strPathFileName.rfind(L'\\', npos))
{
strOutput = strPathFileName.substr(npos+1);
}
return strOutput;
}
희망 당신이 원하는 :
var result=s.Substring(s.LastIndexOf(@"\") + 1);
다음 코드 줄을 사용하여 파일 확장명을 가져올 수 있습니다. 당신은 이름 만 사용 파일이 필요한 경우
string filePath = @"D:\Test\information.xml";
string extention = Path.GetExtension(filePath);
,
string filePath = @"D:\Test\information.xml";
string filename = Path.GetFilename(filePath);
"확장 기능"이란 무엇입니까? – csharpforevermore
이 예제의 경우 확장은 ".xml"입니다. – Sajin
사용 string.Replcae
이string s = @"testpage\information.xml";
s = s.Replace(@"testpage\\",""); // replace 'testpage\' with empty string
당신은 출력이 => S = information.xml
@가 얻을 것이다 문자열에 백 슬래시가 있기 때문에 필요합니다.
충분히 '좋은 10더 STRING에 대한 책을 읽은를 들어
http://www.dotnetperls.com/replace
http://msdn.microsoft.com/en-us/library/system.string.replace.aspx
는'문자열 newstring 인수로 쓰여진 = "information.xml"가 교체? 제발, 당신이 무엇을 필요로 지정 –
그게 항상 파일 이름을 추출하려는 파일 경로가 될거야? –
@ user1727822 더 이상의 문제 또는 문제가 해결 되었습니까? 대답을 확인 했습니까? – Sami