Pads the string with zeros on the left until it has the requested size. : String Pad « Data Type « Java






Pads the string with zeros on the left until it has the requested size.

   
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//package org.ancora.SharedLibrary;

/**
 * Methods for bit manipulation.
 *
 * @author Joao Bispo
 */
public class Util{
     private static final String ZERO = "0";
     private static final String HEX_PREFIX = "0x";
     /**
      * Pads the string with zeros on the left until it has the requested size.
      *
      * @param binaryNumber
      * @param size
      * @return
      */
     public static String padBinaryString(String binaryNumber, int size) {
         int stringSize = binaryNumber.length();
         if(stringSize >= size) {
             return binaryNumber;
         }

         int numZeros = size - stringSize;
         StringBuilder builder = new StringBuilder(numZeros);
         for(int i=0; i<numZeros; i++) {
             builder.append(ZERO);
         }

         return builder.toString() + binaryNumber;
     }
}

   
    
    
  








Related examples in the same category

1.Left pad a String with a specified String.
2.Left pad a String with a specified character.
3.Left pad a String with spaces (' ').
4.Right pad a String with a specified String.
5.Right pad a String with a specified character.
6.Right pad a String with spaces (' ').
7.Padding and triming strings
8.Pad string
9.Padded String
10.Set string length, padding with character if the shorter, or truncating if longer
11.Returns the quoted version of the string using the quotechar argument.
12.Return a string padded with the given string for the given count.
13.Left pad the given text parameter
14.Right pad the given text parameter
15.Left padding
16.Right padding
17.Pads the string with 0x and zeros on the left until it has the requested size.
18.Right, left justify the string