开发者

How to get these files as elegant as possible?

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 t开发者_StackOverflow中文版o 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.


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()


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()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜