468,121 Members | 1,420 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,121 developers. It's quick & easy.

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

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?
--
Alex Moskalyuk
_____________________________
http://www.techinterviews.com
Nov 16 '05 #1
4 3402
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" <us****@moskalyuk.com> wrote in message
news:OM*************@tk2msftngp13.phx.gbl...
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?
--
Alex Moskalyuk
_____________________________
http://www.techinterviews.com

Nov 16 '05 #2
Thanks, using substring just escaped me.

--
Alex Moskalyuk
_____________________________
http://www.techinterviews.com
"John Wood" <sp**@isannoying.com> wrote in message
news:u8**************@TK2MSFTNGP09.phx.gbl...
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" <us****@moskalyuk.com> wrote in message
news:OM*************@tk2msftngp13.phx.gbl...
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?
--
Alex Moskalyuk
_____________________________
http://www.techinterviews.com


Nov 16 '05 #3
John Wood wrote:
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(filenam e);

;)

"Alex Moskalyuk" <us****@moskalyuk.com> wrote in message
news:OM*************@tk2msftngp13.phx.gbl...
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?
--
Alex Moskalyuk
_____________________________
http://www.techinterviews.com


--

Ed Courtenay
[MCP, MCSD]
http://www.edcourtenay.co.uk
Nov 16 '05 #4
Ed Courtenay wrote:
John Wood wrote:
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(filenam e);

;)


Ignore me - I've just re-read the OP; I must remember to read posts more
thoroughly!

"Alex Moskalyuk" <us****@moskalyuk.com> wrote in message
news:OM*************@tk2msftngp13.phx.gbl...
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?
--
Alex Moskalyuk
_____________________________
http://www.techinterviews.com



--

Ed Courtenay
[MCP, MCSD]
http://www.edcourtenay.co.uk
Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.