Can java.lang.String.format(String str, String str1)
be used for adding prefix of a particular character.
I could do this for a number like:
int sendID = 202022;
String usiSuffix = String.format("%032d", sendID);
It makes a String
of length 32 and leftpadded with 0s : 00000000000000000000000000202022
How to achieve the same thing when sendID is a String
like:
String sendID = "AABB";
And I want an output like: 0000000000000000000000000000AABB
In java, String format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string.
Use the String. format() method to pad the string with spaces on left and right, and then replace these spaces with the given character using String. replace() method. For left padding, the syntax to use the String.
In Python, strings have a built-in method named ljust . The method lets you pad a string with characters up to a certain length. The ljust method means "left-justify"; this makes sense because your string will be to the left after adjustment up to the specified length.
padStart() The padStart() method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied from the start of the current string.
You can use this hackish way to get your output:
String sendID = "AABB";
String output = String.format("%0"+(32-sendID.length())+"d%s", 0, sendID);
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