How do I properly align using String.format in Java?

前端 未结 5 1373
梦谈多话
梦谈多话 2021-01-16 09:22

Let\'s say I have a couple variable and I want to format them so they\'re all aligned, but the variables are different lengths. For example:

String a = \"abc         


        
5条回答
  •  梦毁少年i
    2021-01-16 10:20

    private String addEnoughSpacesInBetween(String firstStr, String secondStr){
    
        if (!firstStr.isEmpty() && !secondStr.isEmpty()){
    
            String space = " ";
    
            int totalAllowed = 55;
    
            int multiplyFor = totalAllowed - (firstStr.length() + secondStr.length());
    
            return StringUtils.repeat(space,multiplyFor);
        }
        return null;
    }
    

提交回复
热议问题