How can I split the string to get the file name?

I am trying to break a string. Here is the line.

string fileName = "description/ask_question_file_10.htm" 

I need to remove "description /" and ".htm" from this line. Therefore, as a result, I am looking for "ask_question_file_10". I have to look for "/" and ".htm". I appreciate any help.

+4
source share
4 answers

You can use the Path.GetFileNameWithoutExtension Method :

 string fileName = "description/ask_question_file_10.htm"; string result = Path.GetFileNameWithoutExtension(fileName); // result == "ask_question_file_10" 
+19
source
 string fileName = Path.GetFileNameWithoutExtension("description/ask_question_file_10.htm") 
+5
source

to try

 string myResult = fileName.SubString (fileName.IndexOf ("/") + 1); if ( myResult.EndsWith (".htm" ) ) myResult = myResult.SubString (0, myResult.Length - 4); 

IF this is really the way you can use

 string myResult = Path.GetFileNameWithoutExtension(fileName); 

EDIT - related links:

+2
source
  string fileName = "description/ask_question_file_10.htm"; string name = Path.GetFileNameWithoutExtension(fileName); 
+1
source

All Articles