无法弄清楚这个SubString.PadLeft正在做什么

时间:2022-07-13 21:12:42

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开发,所以设置快速测试对我来说不是那么快。

11 个解决方案

#1


2  

It takes the last two digits of the year and pads the left side with zeroes to a maximum of 2 characters. Looks like a "just in case" for expiration years ending in 08, 07, etc., making sure that the leading zero is present.

它采用年份的最后两位数字,并在左侧填充零,最多为2个字符。在08,07等结束的到期年份看起来像“以防万一”,确保前导零存在。

#2


2  

This prints "98" to the console.

这将“98”打印到控制台。

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

#3


1  

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. Then you'll know for certain.

当然你可以运行它。您无法在正在调试的应用程序中运行它。要了解它正在做什么,而不仅仅是它正在做什么,创建一个新的Web应用程序,放入DropDownList,在其中放置几个​​静态年份,然后输入您提到的代码并查看它是什么确实。然后你肯定会知道。

#4


1  

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'. 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',如果它是零个字符,它返回'00'。我说这个是愚蠢的原因是因为如果你需要两个字符长的值,为什么不设置它就像你创建下拉列表时那样开始?

#5


0  

It looks like it's grabbing the substring from the 3rd character (if 0 based) to the end, then if the substring has a length less than 2 it's making the length equal to 2 by adding 0 to the left side.

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

#6


0  

PadLeft ensures that you receive at least two characters from the input, padding the input (on the left side) with the appropriate character. So input, in this case, might be 12. You get "12" back. Or input might be 9, in which case, you get "09" back.

PadLeft确保您从输入中接收至少两个字符,用适当的字符填充输入(在左侧)。所以输入,在这种情况下,可能是12.你得到“12”。或输入可能是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.

这是一个复杂链接的例子(参见“链接中是否有任何好处”)出错,并使代码看起来过于复杂。

#7


0  

The substring returns the value with the first two characters skipped, the padleft pads the result with leading zeros:

子字符串返回跳过前两个字符的值,padleft将结果填入前导零:

 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位数值。

#8


0  

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.

使用2位数年份(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; 208 -> 08.

如果作者可能认为是拼写错误的3位数年份(207,208),它将返回填充零的最后一位数字 - 207 - > 07; 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就是不必要的 - 子串(2)完全符合您需要的4位数年份。

#9


0  

This code seems to be trying to grab a 2 digit year from a four digit year (ddlexpyear is the hint)

此代码似乎试图从四位数年份中获取2位数年份(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
  • 0: exception
  • 1: exception
  • 2: always returns "00". e.g. the year 49 (when the Jews were expulsed from rome) becomes "00". This is bad.
  • 2:始终返回“00”。例如49年(当犹太人被罗马驱逐出境时)变成“00”。这是不好的。

  • 3: saves the last digit, and puts a "0" in front of it. 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)
  • 3:保存最后一位数字,并在其前面加上“0”。在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.
  • 正如您所料,问题会继续以更高的数字继续。

#10


-1  

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

#11


-1  

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年)。

#1


2  

It takes the last two digits of the year and pads the left side with zeroes to a maximum of 2 characters. Looks like a "just in case" for expiration years ending in 08, 07, etc., making sure that the leading zero is present.

它采用年份的最后两位数字,并在左侧填充零,最多为2个字符。在08,07等结束的到期年份看起来像“以防万一”,确保前导零存在。

#2


2  

This prints "98" to the console.

这将“98”打印到控制台。

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

#3


1  

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. Then you'll know for certain.

当然你可以运行它。您无法在正在调试的应用程序中运行它。要了解它正在做什么,而不仅仅是它正在做什么,创建一个新的Web应用程序,放入DropDownList,在其中放置几个​​静态年份,然后输入您提到的代码并查看它是什么确实。然后你肯定会知道。

#4


1  

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'. 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',如果它是零个字符,它返回'00'。我说这个是愚蠢的原因是因为如果你需要两个字符长的值,为什么不设置它就像你创建下拉列表时那样开始?

#5


0  

It looks like it's grabbing the substring from the 3rd character (if 0 based) to the end, then if the substring has a length less than 2 it's making the length equal to 2 by adding 0 to the left side.

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

#6


0  

PadLeft ensures that you receive at least two characters from the input, padding the input (on the left side) with the appropriate character. So input, in this case, might be 12. You get "12" back. Or input might be 9, in which case, you get "09" back.

PadLeft确保您从输入中接收至少两个字符,用适当的字符填充输入(在左侧)。所以输入,在这种情况下,可能是12.你得到“12”。或输入可能是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.

这是一个复杂链接的例子(参见“链接中是否有任何好处”)出错,并使代码看起来过于复杂。

#7


0  

The substring returns the value with the first two characters skipped, the padleft pads the result with leading zeros:

子字符串返回跳过前两个字符的值,padleft将结果填入前导零:

 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位数值。

#8


0  

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.

使用2位数年份(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; 208 -> 08.

如果作者可能认为是拼写错误的3位数年份(207,208),它将返回填充零的最后一位数字 - 207 - > 07; 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就是不必要的 - 子串(2)完全符合您需要的4位数年份。

#9


0  

This code seems to be trying to grab a 2 digit year from a four digit year (ddlexpyear is the hint)

此代码似乎试图从四位数年份中获取2位数年份(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
  • 0: exception
  • 1: exception
  • 2: always returns "00". e.g. the year 49 (when the Jews were expulsed from rome) becomes "00". This is bad.
  • 2:始终返回“00”。例如49年(当犹太人被罗马驱逐出境时)变成“00”。这是不好的。

  • 3: saves the last digit, and puts a "0" in front of it. 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)
  • 3:保存最后一位数字,并在其前面加上“0”。在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.
  • 正如您所料,问题会继续以更高的数字继续。

#10


-1  

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

#11


-1  

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年)。