zoukankan      html  css  js  c++  java
  • [String]两个右补空格使字符串达到固定长度的函数 来自网上 请君自取

    代码:

    package fixsizestring;
    
    public class TestClass {
        public static void main(String[] args) {
            for(int i=0;i<10;i++) {
                System.out.println(fixSizeStr("#"+i,10)+fixSizeStr("abcdefg",10)+fixSizeStr("北32京32",10)+fixSizeStr("广202州",10)+fixSizeStr(null,10));
                System.out.println(fixSizeStr2("."+i,10)+fixSizeStr2("abcdefg",10)+fixSizeStr2("北32京32",10)+fixSizeStr2("福322州",10)+fixSizeStr2(null,10));
            }
        }
        
        // 得到一定长度的字符串,不足补空格
        private static String fixSizeStr(Object obj, int size) {
            String str;
            if(obj==null) {
                str="null";
            }else {
                str=obj.toString();
            }
            
            int strLen;
            if (obj == null) {
                strLen = 0;
            }else{
                strLen= str.length();
            }
            
            if (strLen == size) {
                return str;
            } else if (strLen < size) {
                int temp = size - strLen;
                String tem = "";
                for (int i = 0; i < temp; i++) {
                    tem = tem + " ";
                }
                return str + tem;
            }else{
                return str.substring(0,size);
            }
        }
        
        // 得到一定长度的字符串,不足补空格
        private static String fixSizeStr2(Object obj, int size) {
            String str;
            if(obj==null) {
                str="null";
            }else {
                str=obj.toString();
            }
            return String.format("%-"+size+"s", str);
        }
    }

    输出:

    #0        abcdefg   北32京32    广202州     null          
    .0        abcdefg   北32京32    福322州     null      
    #1        abcdefg   北32京32    广202州     null          
    .1        abcdefg   北32京32    福322州     null      
    #2        abcdefg   北32京32    广202州     null          
    .2        abcdefg   北32京32    福322州     null      
    #3        abcdefg   北32京32    广202州     null          
    .3        abcdefg   北32京32    福322州     null      
    #4        abcdefg   北32京32    广202州     null          
    .4        abcdefg   北32京32    福322州     null      
    #5        abcdefg   北32京32    广202州     null          
    .5        abcdefg   北32京32    福322州     null      
    #6        abcdefg   北32京32    广202州     null          
    .6        abcdefg   北32京32    福322州     null      
    #7        abcdefg   北32京32    广202州     null          
    .7        abcdefg   北32京32    福322州     null      
    #8        abcdefg   北32京32    广202州     null          
    .8        abcdefg   北32京32    福322州     null      
    #9        abcdefg   北32京32    广202州     null          
    .9        abcdefg   北32京32    福322州     null      

    --END-- 2019-12-06 13:59

  • 相关阅读:
    C# WPF全局捕获异常 防止程序崩溃闪退
    mysql数据库动态创建分区
    mysql增加修改主键_mysql怎么修改添加主键
    C#中@的用法总结(转)
    python OpenCV使用
    turtle --- 海龟绘图¶
    Python 常用趣味模块
    Eclipse中Ant的使用
    Eclipse中Ant的使用
    Thinking in java(五)
  • 原文地址:https://www.cnblogs.com/heyang78/p/11976226.html
Copyright © 2011-2022 走看看