0

I need to separate the file name into an array of two strings.

The file name looks like this: IMG-20190604-WA0005.jpg

An array that I want:

[0] = "IMG-20190604-WA0005"

[1] = "jpg"

I got index position using LasIndexOf('.')

2
  • 2
    string.Substring Commented Jul 28, 2022 at 9:17
  • @derpirscher Can you give an example Commented Jul 28, 2022 at 9:18

1 Answer 1

6

Don't use string methods but the available methods in System.IO.Path:

string file = "IMG-20190604-WA0005.jpg"; string filenameWithoutExtension = Path.GetFileNameWithoutExtension(file); string extension = Path.GetExtension(file); 

If you don't want the . at the beginning, remove it:

string extension = Path.GetExtension(file).TrimStart('.'); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.