简体   繁体   English

无法弄清楚此SubString.PadLeft在做什么

[英]Can't figure out what this SubString.PadLeft is doing

In this code I am debugging, I have this code snipit: 在我正在调试的这段代码中,我有如下代码:

ddlExpYear.SelectedItem.Value.Substring(2).PadLeft(2, '0');

What does this return? 这会返回什么? I really can't run this too much as it is part of a live credit card application. 我真的不能做太多,因为它是实时信用卡应用程序的一部分。 The DropDownList as you could imagine from the name contains the 4-digit year. 您可以从名称中想象出DropDownList包含4位数字的年份。

UPDATE: Thanks everyone. 更新:谢谢大家。 I don't do a lot of .NET development so setting up a quick test isn't as quick for me. 我没有进行太多的.NET开发,因此对我来说设置快速测试并不那么快捷。

It takes the last two digits of the year and pads the left side with zeroes to a maximum of 2 characters. 它使用年份的最后两位数字,并在左侧填充零,最多2个字符。 Looks like a "just in case" for expiration years ending in 08, 07, etc., making sure that the leading zero is present. 对于以08、07等结尾的到期年份,看起来像是“以防万一”,请确保存在前导零。

This prints "98" to the console. 这会将“ 98”打印到控制台。

class Program
{
    static void Main(string[] args)
    {
        Console.Write("1998".Substring(2).PadLeft(2, '0'));
        Console.Read();
    }
}

Of course you can run this. 当然可以运行此程序。 You just can't run it in the application you're debugging. 您只是无法在要调试的应用程序中运行它。 To find out what it's doing, and not just what it looks like it's doing, make a new web application, put in a DropDownList, put a few static years in it, and then put in the code you've mentioned and see what it does. 要弄清它在做什么,而不仅仅是看它在做什么,请制作一个新的Web应用程序,放入一个DropDownList,放入其中一些静态年份,然后放入您提到的代码,然后看一下它的作用。确实。 Then you'll know for certain. 然后,您将确定。

something stupid. 有点愚蠢。 It's getting the value of the selected item and taking the everything after the first two characters. 它获取所选项目的值,并获取前两个字符之后的所有内容。 If that is only one character, then it adds a '0' to the beginning of it, and if it is zero characters, the it returns '00'. 如果那只是一个字符,则在其开头添加一个“ 0”,如果它是零个字符,则返回“ 00”。 The reason I say this is stupid is because if you need the value to be two characters long, why not just set it like that to begin with when you are creating the drop down list? 我之所以说这很愚蠢,是因为如果您需要将该值设为两个字符长,那么为什么在创建下拉列表时不像这样开始设置它呢?

看起来好像是在从第三个字符(如果基于0)到末尾抓取子字符串,然后,如果子字符串的长度小于2,则通过在左侧添加0来使长度等于2。

PadLeft ensures that you receive at least two characters from the input, padding the input (on the left side) with the appropriate character. PadLeft确保您从输入中接收至少两个字符,并在输入(左侧)上填充适当的字符。 So input, in this case, might be 12. You get "12" back. 因此,在这种情况下,输入可能是12。您将得到“ 12”。 Or input might be 9, in which case, you get "09" back. 或输入可能为9,在这种情况下,您将获得“ 09”。

This is an example of complex chaining (see "Is there any benefit in Chaining" post) gone awry, and making code appear overly complex. 这是一个复杂的链接示例(请参阅链接中是否有任何好处”一文 )出了问题,并使代码显得过于复杂。

The substring returns the value with the first two characters skipped, the padleft pads the result with leading zeros: 子字符串将返回值,并跳过前两个字符,左上角的结果将前导零填充为零:

 string s = "2014";
    MessageBox.Show(s.Substring(2).PadLeft(2, 'x')); //14
    string s2 = "14";
    MessageBox.Show(s2.Substring(2).PadLeft(2, 'x')); //xx

My guess is the code is trying to convert the year to a 2 digit value. 我的猜测是代码试图将年份转换为2位数的值。

The PadLeft only does something if the user enters a year that is either 2 or 3 digits long. 仅当用户输入2位数或3位数长的年份时,PadLeft才会执行某些操作。

With a 1-digit year, you get an exception (Subsring errs). 年份为1位数字时,您会遇到一个例外(排除错误)。

With a 2-digit year (07, 08, etc), it will return 00. I would say this is an error. 如果是两位数的年份(07、08等),它将返回00。我会说这是一个错误。

With a 3-digit year (207, 208), which the author may have assumed to be typos, it would return the last digit padded with a zero -- 207 -> 07; 对于作者可能假设是错别字的3位数字年份(207,208),它将返回最后一个数字填充为零-207-> 07; 208 -> 08. 208-> 08。

As long as the user must choose a year and isn't allowed to enter a year, the PadLeft is unnecessary -- the Substring(2) does exactly what you need given a 4-digit year. 只要用户必须选择年份并且不允许输入年份,PadLeft就没有必要了-Substring(2)完全可以满足给定4位数字年份的需求。

This code seems to be trying to grab a 2 digit year from a four digit year (ddlexpyear is the hint) 此代码似乎试图从四位数的年份中获取两位数的年份(ddlexpyear是提示)

It takes strings and returns strings, so I will eschew the string delimiters: 它需要字符串并返回字符串,所以我将避开字符串定界符:

  • 1998 -> 98 1998年-> 98
  • 2000 -> 00 2000-> 00
  • 2001 -> 01 2001-> 01
  • 2012 -> 12 2012-> 12

Problem is that it doesn't do a good job. 问题是它做得不好。 In these cases, the padding doesn't actually help. 在这些情况下,填充实际上并没有帮助。 Removing the pad code does not affect the cases it gets correct. 删除填充码不会影响正确的情况。

So the code works (with or without the pad) for 4 digit years, what does it do for strings of other lengths? 因此,该代码(带或不带填充垫)有效期为4位数年,它对其他长度的字符串有什么作用?

  • null: exception null:异常
  • 0: exception 0:例外
  • 1: exception 1:例外
  • 2: always returns "00". 2:始终返回“ 00”。 eg the year 49 (when the Jews were expulsed from rome) becomes "00". 例如,公元49年(犹太人被驱逐出罗马时)变为“ 00”。 This is bad. 这不好。
  • 3: saves the last digit, and puts a "0" in front of it. 3:保存最后一位数字,并在其前面放置“ 0”。 Correct in 10% of cases (when the second digit is actually a zero, like 304, or 908), but quite wrong in the remainder (like 915, 423, and 110) 在10%的情况下正确(当第二个数字实际上为零时,如304或908),但在其余情况中则完全错误(如915、423和110)
  • 5: just saves the 3rd and 4th digits, which is also wrong, "10549" should probably be "49" but is instead "54". 5:只保存第3位和第4位,这也是错误的,“ 10549”可能应该是“ 49”,而应该是“ 54”。
  • as you can expect the problem continues in higher digits. 如您所料,问题将继续以更高的位数出现。

OK so it's taking the value from the drop down, ABCD 好的,所以它从下拉列表中取值,ABCD

Then it takes the substring from position 2, CD 然后从位置2 CD获得子字符串

And then it err, left pads it with 2 zeros if it needs too, CD 然后它会出错,如果需要的话,请向左填充2个零,CD

Or, if you've just ended X, then it would substring to X and pad to OX 或者,如果您刚刚结束X,则它将子串到X并填充到OX

It's taking the last two digits of the year, then pad to the left with a "0". 它使用一年的最后两位数字,然后在左边加“ 0”。

So 2010 would be 10, 2009 would be 09. 因此,2010年为10,2009年为09。

Not sure why the developer didn't just set the value on the dropdown to the last two digits, or why you would need to left pad it (unless you were dealing with years 0-9 AD). 不知道为什么开发人员不只是将下拉列表中的值设置为最后两位数字,还是不确定为什么您需要将其保留下来(除非您要处理的是公元0-9年)。

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

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