Removing File Extensions (Unsafe)

by Jul 14, 2011

Some users use Trim() to remove file extensions like this:

'c:\test\file.txt'.Trim('.txt')
c:\test\file

This seems to work great, but in reality, Trim() removes all the characters you submitted from the beginning and end of the text. Check this out:

'c:\test\report.txt'.Trim('.txt')
c:\test\repor

The file name is missing the last "t" because "t" is part of the characters Trim() removes.

 

Twitter This Tip!
ReTweet this Tip!