Generate fixed length Strings filled with whitespaces

后端 未结 13 933
借酒劲吻你
借酒劲吻你 2020-12-04 13:52

I need to produce fixed length string to generate a character position based file. The missing characters must be filled with space character.

As an example, the fi

相关标签:
13条回答
  • 2020-12-04 14:20

    For right pad you need String.format("%0$-15s", str)

    i.e. - sign will "right" pad and no - sign will "left" pad

    See my example:

    import java.util.Scanner;
     
    public class Solution {
     
        public static void main(String[] args) {
                Scanner sc=new Scanner(System.in);
                System.out.println("================================");
                for(int i=0;i<3;i++)
                {
                    String s1=sc.nextLine();
                    
                    
                    Scanner line = new Scanner( s1);
                    line=line.useDelimiter(" ");
                   
                    String language = line.next();
                    int mark = line.nextInt();;
                    
                    System.out.printf("%s%03d\n",String.format("%0$-15s", language),mark);
                    
                }
                System.out.println("================================");
     
        }
    }
    

    The input must be a string and a number

    example input : Google 1

    0 讨论(0)
  • 2020-12-04 14:22

    This code works great.

      String ItemNameSpacing = new String(new char[10 - masterPojos.get(i).getName().length()]).replace('\0', ' ');
      printData +=  masterPojos.get(i).getName()+ "" + ItemNameSpacing + ":   " + masterPojos.get(i).getItemQty() +" "+ masterPojos.get(i).getItemMeasure() + "\n";
    

    Happy Coding!!

    0 讨论(0)
  • 2020-12-04 14:23
    String.format("%15s",s) // pads right
    String.format("%-15s",s) // pads left
    

    Great summary here

    0 讨论(0)
  • 2020-12-04 14:25

    Here's a neat trick:

    // E.g pad("sss","00000000"); should deliver "00000sss".
    public static String pad(String string, String pad) {
      /*
       * Add the pad to the left of string then take as many characters from the right 
       * that is the same length as the pad.
       * This would normally mean starting my substring at 
       * pad.length() + string.length() - pad.length() but obviously the pad.length()'s 
       * cancel.
       *
       * 00000000sss
       *    ^ ----- Cut before this character - pos = 8 + 3 - 8 = 3
       */
      return (pad + string).substring(string.length());
    }
    
    public static void main(String[] args) throws InterruptedException {
      try {
        System.out.println("Pad 'Hello' with '          ' produces: '"+pad("Hello","          ")+"'");
        // Prints: Pad 'Hello' with '          ' produces: '     Hello'
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    
    0 讨论(0)
  • 2020-12-04 14:25

    This simple function works for me:

    public static String leftPad(String string, int length, String pad) {
          return pad.repeat(length - string.length()) + string;
        }
    

    Invocation:

    String s = leftPad(myString, 10, "0");
    
    0 讨论(0)
  • 2020-12-04 14:27
    import org.apache.commons.lang3.StringUtils;
    
    String stringToPad = "10";
    int maxPadLength = 10;
    String paddingCharacter = " ";
    
    StringUtils.leftPad(stringToPad, maxPadLength, paddingCharacter)
    

    Way better than Guava imo. Never seen a single enterprise Java project that uses Guava but Apache String Utils is incredibly common.

    0 讨论(0)
提交回复
热议问题