我们可以使用 String.format() 用所需长度的字符填充/前缀吗? [英] Can we use String.format() to pad/prefix with a character with desired length?

查看:11
本文介绍了我们可以使用 String.format() 用所需长度的字符填充/前缀吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java.lang.String.format(String str, String str1)能否用于添加特定字符的前缀.

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

它生成一个长度为 32 并用 0 填充的 String:00000000000000000000000000202022

It makes a String of length 32 and leftpadded with 0s : 00000000000000000000000000202022

当 sendID 是 String 时如何实现相同的功能:

How to achieve the same thing when sendID is a String like:

String sendID = "AABB";

我想要这样的输出:0000000000000000000000000000AABB

推荐答案

您可以使用这种骇人听闻的方式来获取输出:

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

这篇关于我们可以使用 String.format() 用所需长度的字符填充/前缀吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆