java字符串拼接指定内容-左右组合(left/right padding)
来源:互联网 发布:我的世界随机性知乎 编辑:程序博客网 时间:2024/06/05 02:22
今天在处理ID时,又一次碰到了字符串的左右拼接问题。相信很多人都有经历。
我们还是熟悉下需求,还是那句话。需求相同才是可用的解决方案。
我们需要在某个字符串的左边填充0,总长度为10位,或者右边填充0,总长度为0,或者,前后的内容固定,中间需要填充0.
String没有left padding ,right padding的函数。。那只有自己写了。。
代码性能没有测试,但是自己感觉还行。
上代码。(英语较烂,凑合看)
/** * get left padding String with specification char for existed suffix * for example,if need left padding zero for some String, * like left padding 0 to 12,and need total length 10, * then the result will be 0000000012. * * @param suffix * the suffix for return String * @param fill * the fill char for String * @param totalLength * the total length need for composite String * @return */public static String getLeftPadding(String suffix,char fill,int totalLength){return getFillString("",suffix,fill,totalLength);}/** * get right padding String with specification char for existed prefix * for example,if need left padding zero for some String, * like right padding 0 to A,and need total length 10, * then the result will be A000000000. * * @param prefix * the prefix for return String * @param fill * the fill char for String * @param totalLength * the total length need for composite String * @return */public static String getRightPadding(String prefix,char fill,int totalLength){return getFillString(prefix,"",fill,totalLength);}/** * get char fill composite String for existed prefix or suffix * for example,if need left padding zero for some String, * like padding 0 to 12,and need total length 10, * then the result will be 0000000012. what's more, * if add a prefix for this composite String,like A, * then then return String will be A000000012. * * @param prefix * the prefix for return String * @param suffix * the suffix for return String * @param fill * the fill char for String * @param totalLength * the total length need for composite String * @return */public static String getFillString(String prefix,String suffix,char fill,int totalLength){int c = 0;int needLength = totalLength - (prefix.length()+suffix.length());StringBuilder sb = new StringBuilder();sb.append(prefix);while(c < needLength){sb.append(fill);c++;}sb.append(suffix);return sb.toString();}public static void main(String[] args) {System.out.println(getFillString("A","1",'0',10));System.out.println(getLeftPadding("1",'0',10));System.out.println(getRightPadding("A",'0',10));}
阅读全文
0 0
- java字符串拼接指定内容-左右组合(left/right padding)
- 手机端页面padding-left,padding-right或margin-left,margin-right会使页面左右摇晃
- 左右连接(left,right)及等值连接(inner)
- Excel字符串截取(left&right&mid)
- 数据库左右(left,right)连接及多表连接(inner)
- IE下input text在字符超出文本框长度时,padding-left和padding-right失效
- IE下input text在字符超出文本框长度时,padding-left和padding-right失效
- 【记】IE下input标签中的padding-left和padding-right
- navigationItem左右按钮偏移的问题(left,right)
- access截取字符串,Left 函数,Right 函数
- sql 保留部分字符串left right substring
- mysql 的截取字符串 left right
- SAP HANA LEFT/RIGHT字符串截取
- 按照指定格式拼接字符串
- C# 路径拼接(将多个字符串组合成一个路径)
- a标签有左右padding(没有上下padding)
- left-right
- margin-left:(外边距)与padding-left:(内边距)
- SpringBoot15-springboot核心-Spring Boot运行原理
- cin/cout 效率问题
- mongodb在java中常见的查询(一)
- Java final修饰符
- java中的sleep()和wait()的区别
- java字符串拼接指定内容-左右组合(left/right padding)
- Android okhttp3 post get put 提交
- 从0开始学习 GitHub 系列之【了解Git】
- 安装Composer PHP Warning: copy(): SSL operation failed with code 1.
- Nginx和CORS
- 如何建设高质量外链的烦恼
- SQL:判断字段是否为空或空字符串
- 洛谷 1029 最大公约数和最小公倍数问题
- Spring Boot Spring Data Jpa全解之分页特殊查询