Answer the question
In order to leave comments, you need to log in
How will it be faster to get the file extension?
How will it be faster:
string ext = System.IO.Path.GetExtension(FileName);
string ext=FileName.Substring(FileName.LastIndexOf('.'));
System.IO.FileInfo fi = new System.IO.FileInfo(FileName);
string ext = fi.Extension;
string[] temp= FileName.Split('.');
string ext =temp[temp.Length-1];
System.Text.RegularExpressions.Regex extend = new System.Text.RegularExpressions.Regex(@"(?:.*\.)(.*)");
string ext = extend.Match(FileName).Groups[1].Value;
Answer the question
In order to leave comments, you need to log in
c.w.e - conditional time unit XD
1 place (123 c.w.e):
2nd place (315 c.u.e.):
3rd place (519 c.u.e.):
string[] temp= FileName.Split('.');
string ext =temp[temp.Length-1];
System.IO.FileInfo fi = new System.IO.FileInfo(FileName);
string ext = fi.Extension;
System.Text.RegularExpressions.Regex extend = new System.Text.RegularExpressions.Regex(@"(?:.*\.)(.*)");
string ext = extend.Match(FileName).Groups[1].Value;
string ext=FileName.Substring(FileName.LastIndexOf('.'));
string[] temp= FileName.Split('.');
string ext =temp[temp.Length-1];
System.Text.RegularExpressions.Regex extend = new System.Text.RegularExpressions.Regex(@"(?:.*\.)(.*)");
string ext = extend.Match(FileName).Groups[1].Value;
Some_path/fome_folder_with.dot/some_file_without_extention
or
some_path/.some_name_started_with_dot
do not take into account such a case .
The latter files cannot be created in Windows but can be used, many cross-platform programs sin by creating such "foreign" paths.
add another option
Regex extend = new Regex(@"(?:.*\.)(.*)");
string ext = extend.Match(FileName).Groups[1].Value
Tested):
System.IO.Path.GetExtension: ext=.txt milSec=00:00:00.0000016
Substring: ext=.txt milSec=00:00:00.0000019
System.IO.FileInfo: ext=.txt milSec=00:00 :00.0000445
Split: ext=txt milSec=00:00:00.0000009
System.Text.RegularExpressions.Regex: ext=txt milSec=00:00:00.0004294
=)
What do you use for? I mean, how often will it run? If it is rare, for example, on a mouse click, then it does not matter at all how long this operation takes, and the clarity of the command will be more important in terms of further support. Therefore, Path.GetExtension() is the best choice.
And if you have this command called many times in a cycle, and its performance is really important, only then you should think about how to get data.
The general rule is to write as clear and obvious code as possible, and where performance is needed, only there you need to optimize. Premature optimization is evil, because it takes a lot of programmer time.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question