Monday, July 23, 2018

Use PowerShell to Get the Length of an Integer

PowerShell can return the length of a string easily. Wrap the string in parentheses and the the Length property of the object using dot notation:

("Some strings are rather lengthy").Length



But what about integers?

 

That's because the Length property is counting the integer as a whole rather than looking at the individual units. If we're going to say, "Hey PowerShell, use this string method to get the length of what's in between the parens", then PowerShell is going to assume it's a string, not an integer.

To get around this, we can just convert the integer to a string using the 'ToString()' method:

(6548138735138).ToString().Length

 

 
 

No comments:

Post a Comment