开发者

Explain me this python expression in C#

What is the C# equivalent of this python expression?

file_no = int (last_file.Name.Replace(last_file.Extension,"")[-3:]);

I underst开发者_如何学Goand what [-3:] does but not the (int) cast.


It replaces the extension of a filename with the empty string, takes the last three characters as a string and tries to convert to an int.

So if last_file is jamesbond007.secretagent and last_file.Extension is .secretagent then file_no becomes 7 because .secretagent is replaced by the empty string, and the last three characters are 007 which is parsed into 7.

int file_no = 
    Int32.Parse(
        last_file.Name.Replace(
            last_file.Extension, String.Empty
        )
        .Right(3)
    );


Passing a value to the int constructor will attempt to turn it into a int (or long, if it is too big). Think Convert.ToInt32().


I cannot tell you the answer in C#, but I can explain what this expression does: it takes the extension from a filename and then the last 3 characters and tries to convert these to int.

E.g. measure009.bin -> measure009 -> 009 -> 9.


Because you say 'the int cast' I will assume you already know the value is being converted to an int. The reason is that before the cast the value is a string representation of an int and python will not let you use a 'string int' as an int.

oh, and it's python; you don't need to be obsessed with ';' line endings anynmore ;P


It's not really a cast. int converts the argument into an integer, so the closest C# expression would be int.Parse(...) or a call using a converter.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜