左填充
来源:互联网 发布:招商迪辰软件 编辑:程序博客网 时间:2024/03/29 20:02
实现一个leftpad库,如果不知道什么是leftpad可以看样例
样例
leftpad("foo", 5)>> " foo"leftpad("foobar", 6)>> "foobar"leftpad("1", 2, "0")>> "01"
String对象是不可改变的。每次使用 System.String类中的方法之一时,都要在内存中创建一个新的字符串对象,这就需要为该新对象分配新的空间。在需要对字符串执行重复修改的情况下,与创建新的 String对象相关的系统开销可能会非常昂贵。如果要修改字符串而不创建新的对象,则可以使用System.Text.StringBuilder类。例如,当在一个循环中将许多字符串连接在一起时,使用 StringBuilder类可以提升性能【动态对象】。
/**
* @param originalStr the string we want to append to with spaces
* @param size the target length of the string
* @return a string
*/
static public String leftPad(String originalStr, int size) {
// Write your code here
return leftPad(originalStr,size,' ');
}
/**
* @param originalStr the string we want to append to
* @param size the target length of the string
* @param padChar the character to pad to the left side of the string
* @return a string
*/
static public String leftPad(String originalStr, int size, char padChar) {
// Write your code here
int len = originalStr.length();
StringBuilder str = new StringBuilder();
for(int i=0;i<(size-len);i++)
str.append(padChar);
return str.append(originalStr).toString();
}
}
0 0
- 左填充
- 左填充
- 左填充
- 左填充
- Oracle 左填充 右填充
- java 左填充
- LintCode - 524.左填充
- 【lintcode】leftpad 左填充
- 左填充-LintCode
- LintCode_524_左填充
- oracle左填充lpad右填充rpad
- 524.Left Pad-左填充(容易题)
- 数据结构实例<七>(Left-Pad 左填充)容易
- Java中数字转换为固定长度的字符串,并左填充为0
- PHP之生成左填充0的编号(存储到MYSQL)
- LintCode算法题解——移动零、玩具工厂、左填充、丑数
- java技术面试:给你一个字符串str和整形数len,然后在str左填充"0",直到str的位数等于len为止。
- 写一函数,实现用某一字符c 去填充字符串的左部(右部),使其长度为恰好为length
- web前端-JavaScript 保留关键字 -026
- ES6学习17(SIMD)
- IntelliJ IDEA 中详细图解记录如何连接MySQL数据库
- ORACLE initialization or shutdown in progress
- 静态网页
- 左填充
- android Scrollview里的内容不停留在顶部的问题
- 单元测试那些事
- 奇偶剪枝问题 深搜搜到满足条件的路径时,不再搜其他路径
- LCD常用接口原理
- Python统计学一数据的概括性度量
- Java基础学习总结(8)——Java23种设计模式(Design Patterns)详解
- LeetCode217 Contains Duplicate
- 织梦采集侠采集问题