简体   繁体   English

替换列表中的扩展名<String>

[英]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. 我有以下字符串列表,我想用String.Empty删除/替换字符串的最后一部分(即“ .mp4 \\ n”)。

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. 我正在使用.NET 4.0,但对C#和.NET还是陌生的。

Use Path.GetFileNameWithoutExtension Method like: 使用Path.GetFileNameWithoutExtension方法,例如:

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

Use string.Trim if your string ends with \\n 如果您的字符串以\\n结尾,请使用string.Trim

Or Shorter form (if the file name doesn't contain '\\n`): 或更短的格式(如果文件名不包含'\\ n`):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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