如果一个字符串长度规定为20
在里面从左往右填充字符 不够20个则用空格补满 最简单的代码和最高效的代码分别是?
如果需求变为空格在字符串左边填充又如何实现?
谢谢!
在里面从左往右填充字符 不够20个则用空格补满 最简单的代码和最高效的代码分别是?
如果需求变为空格在字符串左边填充又如何实现?
谢谢!
解决方案 »
- 接口中的方法加和不加abstract有啥区别?(冒着被鄙视风险)
- 找错题 看看这段代码中哪里有错误,先谢谢帮助菜鸟的老鸟们~~~
- 关于制作安装程序
- 请问哪里有installshied下载啊?非常急,没分了,不好意思啊!
- 做jsp页面 什么时候才能用到线程?
- Blob 提问
- 我是个新手,加入到java队伍里来.大家帮帮忙,扶持一下小弟吧!!!
- 在线急等!求救json操作
- 我想在java程序运行时修改cmd.exe文件的属性(如显示颜色等)应怎么作?谢谢!!!
- Applet 的发布问题?(我已经搜索过了,没找到答案!)
- 有关 XML 名字空间定义的问题
- 我想学习java socket编程。不知道在哪里可以找到比较多的资料呢?
字符串a+“20空格” 然后substring
然后通过StringBuffer来循环添加
String str="123";
System.out.println("右补空:["+String.format("%1$-20s", str)+"]");
System.out.println("左补空:["+String.format("%1$20s", str)+"]"); }}
String.format("%4$2s %3$2s %2$2s %1$2s", "a", "b", "c", "d")
// -> " d c b a" // Optional locale as the first argument can be used to get
// locale-specific formatting of numbers. The precision and width can be
// given to round and align the value.
String.format("e = %+10.4f", Math.E);
// -> "e = +2,7183" // The '(' numeric flag may be used to format negative numbers with
// parentheses rather than a minus sign. Group separators are
// automatically inserted.
String.format("Amount gained or lost since last statement: $ %(,.2f",
balanceDelta);
// -> "Amount gained or lost since last statement: $ (6,217.58)"
面向组件的软件开发, 专注net, java技术.
探讨新一代软件特征webOS, 第三代搜索引擎技术,P2P,Grid,动态语言, AJAX, 虚拟操作系统
简单的话,这样行不行
字符串a+“20空格” 然后substring
///////////////////////////////////////////////////////////
我觉得这样很好
System.out.println("右补空:["+String.format("%1$-20s", str)+"]");没看懂那个20s的s是什么意思 能说明下吗?谢谢
%1$-20s的含义是“%1$”第一个参数,“-20s”以右补空最少20个字符的方式转换
如果是20s就是左补空最少20个字符的方式转换