简体   繁体   中英

Replace extensions in List<String>

I have the following list of strings where I would like to remove/replace the last part (ie “.mp4\\n”) of the string with String.Empty.

List<String> _response
[0] ---- “test.mp4\n”
[1] ---- “test2.mov\n”
[3] ---- “test3.mp4\n”
[4] ---- “test3.mp3\n”
etc.

How can I remove the extensions in a smart and simple way? I'm using .NET 4.0 and I'm new to C# and .NET.

Use Path.GetFileNameWithoutExtension Method like:

var newList = _response.Select(r=> Path.GetFileNameWithoutExtension(r.Trim())).ToList();

Use string.Trim if your string ends with \\n

Or Shorter form (if the file name doesn't contain '\\n`):

var newList = _response.Select(Path.GetFileNameWithoutExtension).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM