如何让这些文件尽可能优雅?

发布于 2024-10-22 07:22:13 字数 711 浏览 9 评论 0原文

我有一些以下形式的文件:

blur.static.shadereffect
blur.dynamic.shadereffect
blur.virtual.shadereffect

soften.static.shadereffect
soften.dynamic.shadereffect
soften.virtual.shadereffect

median.static.shadereffect
median.dynamic.shadereffect
median.virtual.shadereffect

...

现在我正在获取 .static.shadereffect 文件,然后过滤掉最后 2 部分,因此只有名称存在,例如 "blur"、"soften "、"median" 等。

某些着色器效果可以有更多或更少的类型,因此我不想硬编码 .static.shadereffect

因此,最终该方法将返回 .shadereffect 文件的名称:

{"blur", "soften", "median"}

如何使用尽可能少的代码最优雅地完成此操作?性能并不重要。

编辑:一个小细节。文件名也可以有超过 2 个点,例如“blur.sharpen.dynamic.shadereffect”,这不会影响结果。

I have some files in the form of:

blur.static.shadereffect
blur.dynamic.shadereffect
blur.virtual.shadereffect

soften.static.shadereffect
soften.dynamic.shadereffect
soften.virtual.shadereffect

median.static.shadereffect
median.dynamic.shadereffect
median.virtual.shadereffect

...

Right now I am getting the .static.shadereffect files and then filtering out the last 2 parts so only the name exists like "blur", "soften", "median", etc.

Some shader effects can have more or less types so I don't want to hard code .static.shadereffect.

So in the end the method will return the names of the .shadereffect files:

{"blur", "soften", "median"}

How do I do this most elegantly with as little code as possible? Performance is not important.

EDIT: A small detail. The file names can also have more than 2 dots, so something like "blur.sharpen.dynamic.shadereffect", which shouldn't throw off the results.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木緿 2024-10-29 07:22:13

我只会对每个文件名使用 string.Split ,然后使用 Distinct :

    files
    .Select( filename => filename.Split( '.' )[0] )
    .Distinct()

尽管如此,我必须承认,这可能不是最有效的方法。如果你的名字很长并且有很多点,这会浪费一些内存和时间。更好的方法是显式获取字符串到第一个点的部分:

    files
    .Select( filename => new string( filename.TakeWhile( c => c != '.' ).ToArray() ) )
    .Distinct()

I would just use string.Split for each filename, and then Distinct:

    files
    .Select( filename => filename.Split( '.' )[0] )
    .Distinct()

Although, I must admit, this may not be the most efficient way. If you have long names with many dots, this will waste some memory and time. A better way would be to explicitly take the portion of the string up to the first dot:

    files
    .Select( filename => new string( filename.TakeWhile( c => c != '.' ).ToArray() ) )
    .Distinct()
江挽川 2024-10-29 07:22:13

我没有测试它,所以某处可能存在一些相差一的错误。但这应该选择除每个字符串的最后两个部分之外的所有内容。

files
    .Select( s=> 
      {
         int dot1=s.LastIndexOf(".");
         int dot2=s.LastIndexOf(".",dot1-1);
         s.SubString(0,dot2-1);
      }
     )
    .Distinct()

I didn't test it, so there might be some off-by-one bug somewhere. But this should select everything except the last two parts of each string.

files
    .Select( s=> 
      {
         int dot1=s.LastIndexOf(".");
         int dot2=s.LastIndexOf(".",dot1-1);
         s.SubString(0,dot2-1);
      }
     )
    .Distinct()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文