5

I am trying to split the string. Here is the string.

string fileName =   "description/ask_question_file_10.htm"

I have to remove "description/" and ".htm" from this string. So the result I am looking for "ask_question_file_10". I have to look for "/" and ".htm" I appreciate any help.

4 Answers 4

21

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"
Sign up to request clarification or add additional context in comments.

Comments

5
string fileName = Path.GetFileNameWithoutExtension("description/ask_question_file_10.htm")

Comments

2

try

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

IF it is really a path then you can use

string myResult = Path.GetFileNameWithoutExtension(fileName);

EDIT - relevant links:

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.