Introduction. In Python, the string is an immutable object. You can use the '+' operator to append two strings to create a new string. There are various ways such as using join, format, string IO, and appending the strings with space.
The format() method of String class in Java 5 is the first choice. You just need to add "%03d" to add 3 leading zeros in an Integer. Formatting instruction to String starts with "%" and 0 is the character which is used in padding. By default left padding is used, 3 is the size and d is used to print integers.
+ ""
evaluates to the number 0
. This is because in order to apply the unary plus operator, ""
is coerced to a number--the same as Number("")
, which results in 0
if the string is empty or blank.
When you then "add" (+
or in this case +=
) the number 0
to the string "abc"
, it is coerced to the string "0"
, resulting in "abc0"
.
From the spec:
A StringNumericLiteral that is empty or contains only white space is converted to +0.
As you can see in TypeScript (+ "")
is a number (0):
from: http://xkr.us/articles/javascript/unary-add/
In JavaScript it is possible to use the + operator alone before a single element. This indicates a math operation and tries to convert the element to a number. If the conversion fails, it will evaluate to NaN. This is especially useful when one wants to convert a string to a number quickly, but can also be used on a select set of other types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With