Quick hack - getting the first word in the filename before the dot

  • Thread starter Thread starter Alex Moskalyuk
  • Start date Start date
A

Alex Moskalyuk

We have a filename, which could theoretically be something like
verylongfilename.2004.05.11.txt. We just need the significant part before
the first dot. My solution was a quick regexp:

filename = (Regex.Split(filename, "."))[0];

Is there a better way to handle it? Maybe something built in?
 
Firstly you don't need regex... just filename.Split('.')[0] would suffice.
Secondly you could just use substring, as in...
filename = filename.Substring(0, filename.IndexOfAny('.')-1)
 
Thanks, using substring just escaped me.

--
Alex Moskalyuk
_____________________________
http://www.techinterviews.com
John Wood said:
Firstly you don't need regex... just filename.Split('.')[0] would suffice.
Secondly you could just use substring, as in...
filename = filename.Substring(0, filename.IndexOfAny('.')-1)

Alex Moskalyuk said:
We have a filename, which could theoretically be something like
verylongfilename.2004.05.11.txt. We just need the significant part before
the first dot. My solution was a quick regexp:

filename = (Regex.Split(filename, "."))[0];

Is there a better way to handle it? Maybe something built in?
 
John said:
Firstly you don't need regex... just filename.Split('.')[0] would suffice.
Secondly you could just use substring, as in...
filename = filename.Substring(0, filename.IndexOfAny('.')-1)

Thirdly, you could make life *really* easy by using:

filename = System.IO.Path.GetFileNameWithoutExtension(filename);

;)
We have a filename, which could theoretically be something like
verylongfilename.2004.05.11.txt. We just need the significant part before
the first dot. My solution was a quick regexp:

filename = (Regex.Split(filename, "."))[0];

Is there a better way to handle it? Maybe something built in?
 
Ed said:
John said:
Firstly you don't need regex... just filename.Split('.')[0] would
suffice.
Secondly you could just use substring, as in...
filename = filename.Substring(0, filename.IndexOfAny('.')-1)


Thirdly, you could make life *really* easy by using:

filename = System.IO.Path.GetFileNameWithoutExtension(filename);

;)

Ignore me - I've just re-read the OP; I must remember to read posts more
thoroughly!
We have a filename, which could theoretically be something like
verylongfilename.2004.05.11.txt. We just need the significant part
before
the first dot. My solution was a quick regexp:

filename = (Regex.Split(filename, "."))[0];

Is there a better way to handle it? Maybe something built in?
 
Back
Top