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(); } }
  • 相关阅读:
    5月29 流程
    5月27 权限设置及功能
    5月26 留言板练习题
    5月24 文件操作
    5月23 文件上传及图片上传预览
    5月23 注册审核
    5月21 回话控制SESSION COOKIE
    5月21 汽车查询及批量删除----php方法
    5月21 练习AJAX的查看详细及批量删除
    5月20 三级联动
  • 原文地址:https://www.cnblogs.com/theskulls/p/5650124.html
Copyright © 2011-2022 走看看