zoukankan      html  css  js  c++  java
  • lintcode: 左填充


    题目

    实现一个leftpad库,如果不知道什么是leftpad可以看样例

    
    
    样例
    leftpad("foo", 5)
    >> "  foo"
    
    leftpad("foobar", 6)
    >> "foobar"
    
    leftpad("1", 2, "0")
    >> "01"
    解题
    public
    class StringUtils { /** * @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 int n = originalStr.length(); if(n>=size) return originalStr; int k = size - n; StringBuffer newStr = new StringBuffer(); while(k>=1){ newStr.append(' '); k--; } newStr.append(originalStr); return newStr.toString(); } /** * @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 n = originalStr.length(); if(n>=size) return originalStr; int k = size - n; StringBuffer newStr = new StringBuffer(); while(k>=1){ newStr.append(padChar); k--; } newStr.append(originalStr); return newStr.toString(); } }
  • 相关阅读:
    MyBatis学习之输入输出类型
    MyBatis学习之多表查询
    javascript学习之this
    Elasticsearch学习之Java操作1
    CSS学习之定位
    CSS学习之浮动
    CSS学习之盒子模型
    java学习之导出Excel
    转载:手把手教你做iOS推送
    拳头公司聊天服务架构:服务器篇
  • 原文地址:https://www.cnblogs.com/bbbblog/p/5650124.html
Copyright © 2011-2022 走看看