0

My code:

string strFilePath = textBox1.text;

The textbox's text typically looks like C:\today\abc def.txt.

I want to isolate 'abc def' into its own string. That is, everything before '.txt' but after the last '\'.

The string manipulation that I'm familiar with uses Split and Last, but neither are applicable here (I think).

3
  • Actually multiple splits would work fine here. Commented Oct 4, 2013 at 19:59
  • 1
    Is it abc def.txt the name of the file or the folder is called abc and the file def.txt? Commented Oct 4, 2013 at 20:00
  • Shouldn't you use the standard open file dialog? Commented Oct 4, 2013 at 20:03

2 Answers 2

8

You're looking for Path.GetFileNameWithoutExtension().

Sign up to request clarification or add additional context in comments.

2 Comments

In the current context, this is a bullseye
Yes, this is exactly what I was hoping/looking for. Thank you!
0

You can use String.LastIndexOf and String.SubString methods like;

string s = @"C:\today\abc def.txt";
string ss = s.Substring(s.LastIndexOf('\\') + 1, s.IndexOf('.') - s.LastIndexOf('\\') - 1);
Console.WriteLine(ss);

Output will be;

abc def

Here a DEMO.

Or easy way, just use Path.GetFileNameWithoutExtension method

Returns the file name of the specified path string without the extension.

string name = Path.GetFileNameWithoutExtension(@"C:\today\abc def.txt");
Console.WriteLine(name); //abc def

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.