12

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

anubhava
  • 664,788
  • 59
  • 469
  • 547
Swagatika
  • 3,300
  • 5
  • 28
  • 38
  • Many solutions here - [How to format a java string with leading zero?](http://stackoverflow.com/questions/4051887/how-to-format-a-java-string-with-leading-zero) – adarshr Mar 26 '13 at 10:58
  • http://stackoverflow.com/a/391978/2123124 looks like what you need i believe – FridayChils Mar 26 '13 at 10:59
  • 1
    @adarshr I have already looked into that answer, that does not answer my question. – Swagatika Mar 26 '13 at 11:07

5 Answers5

17

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);

Demo: http://ideone.com/UNVjqS

David Newcomb
  • 10,026
  • 3
  • 42
  • 56
anubhava
  • 664,788
  • 59
  • 469
  • 547
  • 1
    For anybody wanting to pad a string with trailing zeroes, you can use a very similar approach. String.format("%s"+"%0"+(32-sendID.length())+"d", sendID, 0); – k.schroeder31 Jan 14 '15 at 20:47
12

You can do as below if you really want to use String.format,

String sendID = "AABB";
String.format("%32s", sendID ).replace(' ', '0')

Other than String.format you can find many solutions here.

Edit: Thanks for Brian to point of the issue. The above wont work for input with spaces. You can try as below. But I wont suggest the below operation as it has too many string operation.

String sendID = "AA BB";
String suffix = String.format("%32s", "").replace(' ', '0') + sendID;
sendID = suffix.substring(sendID.length());

You can also try using StringUtils.leftPad

StringUtils.leftPad(sendID, 32 - sendID.length(), '0');
Community
  • 1
  • 1
Jayamohan
  • 12,059
  • 2
  • 25
  • 39
5

You can't use String.format() for padding with arbitrary characters. Perhaps Apache Commons StringUtils.leftPad() would be of use for a concise solution ? Note there's also a StringUtils.rightPad() too.

Michael
  • 34,340
  • 9
  • 58
  • 100
Brian Agnew
  • 254,044
  • 36
  • 316
  • 423
2

I just add some Java 8 code if someone need it in future:

public class App {
    public static void main(String[] args) {
        System.out.println(leftpad("m m", 2, '@'));
        System.out.println(leftpad("m m", 5, '@'));
    }

    static String leftpad(String s, int nb, char pad) {
        return Optional.of(nb - s.length())
                .filter(i -> i > 0)
                .map(i-> String.format("%" + i + "s", "").replace(" ", pad + "") + s)
                .orElse(s);
    }

}

This version supports adding any char as padding

Koziołek
  • 2,570
  • 25
  • 37
  • Is there a reason you do `replace(" ", pad+"")` (i.e. using the substring signature of `replace`) rather than simply `replace(' ', pad)`? – Ti Strga Jan 07 '19 at 16:15
  • No. Just write in IDE and `replace(" ", pad+"")` was „faster” in type. – Koziołek Jan 09 '19 at 12:31
0

This is how I solved this issue using the base jdks String.format function

String sendId="AABB";
int length=20;
String.format("%"+length+"s",sendId).replaceAll(" ","0")
  • 1
    Be aware that this will change any spaces within sendID to zeroes, which may cause problems depending on your domain. – k.schroeder31 Jan 14 '15 at 20:50