Java String align right

Description

Java String align right

public class Main {
  public static void main(String[] argv) throws Exception {
    String str = "demo2s.com";
    int size = 20;
    System.out.println(">" + alignRight(str, size) + "<");
  }//  w  w  w . j  av a2s .c  o  m

  public static final String EMPTY_STRING = "";

  public static String alignRight(String str, int size) {
    return alignRight(str, size, ' ');
  }

  public static String alignRight(String str, int size, char padChar) {
    if (str == null) {
      return null;
    }

    int pads = size - str.length();

    if (pads <= 0) {
      return str;
    }

    return alignRight(str, size, String.valueOf(padChar));
  }

  public static String alignRight(String str, int size, String padStr) {
    if (str == null) {
      return null;
    }

    if ((padStr == null) || (padStr.length() == 0)) {
      padStr = " ";
    }

    int padLen = padStr.length();
    int strLen = str.length();
    int pads = size - strLen;

    if (pads <= 0) {
      return str;
    }

    if (pads == padLen) {
      return padStr.concat(str);
    } else if (pads < padLen) {
      return padStr.substring(0, pads).concat(str);
    } else {
      char[] padding = new char[pads];
      char[] padChars = padStr.toCharArray();

      for (int i = 0; i < pads; i++) {
        padding[i] = padChars[i % padLen];
      }

      return new String(padding).concat(str);
    }
  }

}

//package com.demo2s;

public class Main {
   public static void main(String[] argv) throws Exception {
      String str = "demo2s.com";
      int size = 30;
      System.out.println(">" + alignRight(str, size) + "<");
   }/*from w ww.  j  a va  2s  . c o m*/

   public static String alignRight(String str, int size) {
      return alignRight(str, size, ' ');
   }

   public static String alignRight(String str, int size, char padChar) {
      if (str == null) {
         return null;
      } else {
         int pads = size - str.length();
         return pads <= 0 ? str : alignRight(str, size, String.valueOf(padChar));
      }
   }

   public static String alignRight(String str, int size, String padStr) {
      if (str == null) {
         return null;
      } else {
         if (padStr == null || padStr.length() == 0) {
            padStr = " ";
         }

         int padLen = padStr.length();
         int strLen = str.length();
         int pads = size - strLen;
         if (pads <= 0) {
            return str;
         } else if (pads == padLen) {
            return padStr.concat(str);
         } else if (pads < padLen) {
            return padStr.substring(0, pads).concat(str);
         } else {
            char[] padding = new char[pads];
            char[] padChars = padStr.toCharArray();

            for (int i = 0; i < pads; ++i) {
               padding[i] = padChars[i % padLen];
            }

            return (new String(padding)).concat(str);
         }
      }
   }

}

//package com.demo2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        Object str = "demo2s.com";
        int size = 30;
        System.out.println(">"+alignRight(str, size)+"<");
    }//from  www  .  jav  a  2 s  . c o  m

    public static String alignRight(final Object str, final int size) {
        return alignRight(str.toString(), size, ' ');
    }

    public static String alignRight(final CharSequence str, final int size, final char fill) {
        final char[] chars = new char[size];
        final int fillSize = size - str.length();
        if (fillSize < 0)
            throw new IllegalArgumentException("Does not fit.");
        for (int i = 0; i < fillSize; i++)
            chars[i] = fill;
        for (int i = 0; i < str.length(); i++)
            chars[fillSize + i] = str.charAt(i);
        return String.copyValueOf(chars);
    }
}

//package com.demo2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String data = "demo2s.com";
        int lineLength = 30;
        System.out.println(alignRight(data, lineLength));
    }//from ww w  .  j  a v  a2s.c  o m

    public static String alignRight(String data, int lineLength) {
        int len = Math.min(data.length(), lineLength);
        String s = data.substring(0, len);
        len = lineLength - len;
        for (int i = 0; i < len; i++) {
            s = " " + s;
        }
        return s;
    }
}



PreviousNext

Related