Breaking News
Home / Java / Java Snippets / Write a prepend String function in Java?

Write a prepend String function in Java?

  1.  /**
  2.      * Method to PrePend String
  3.      */
  4.     public String prependString(String source, String pad, int length)
  5.     {
  6.         if (source == null)
  7.         {
  8.             return null;
  9.         }
  10.  
  11.         if (pad == null)
  12.         {
  13.             return source;
  14.         }
  15.  
  16.         StringBuffer result = new StringBuffer();
  17.  
  18.         result.append(source);
  19.  
  20.         while (result.length() < length)
  21.         {
  22.             result.insert(0, pad);
  23.         }
  24.  
  25.         return result.toString();
  26.     }
[pb_builder]

Check Also

How to progamatically get the Java version?

Snippet The key here is to use the system property java.version and get minor and …

Leave a Reply