Java String Align Right alignRight(String str, int length, boolean isEllipsis)

Here you can find the source of alignRight(String str, int length, boolean isEllipsis)

Description

align Right

License

Apache License

Declaration

public static String alignRight(String str, int length,
            boolean isEllipsis) 

Method Source Code

//package com.java2s;
/*//from w w w . ja  va2  s  . c  o  m
 * Copyright 2008-2009 MOPAS(Ministry of Public Administration and Security).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    private final static char WHITE_SPACE = ' ';

    public static String alignRight(String str, int length) {

        return alignRight(str, length, false);
    }

    public static String alignRight(String str, int length,
            boolean isEllipsis) {

        if (str.length() <= length) {

            StringBuffer temp = new StringBuffer(length);
            for (int i = 0; i < (length - str.length()); i++) {
                temp.append(WHITE_SPACE);
            }
            temp.append(str);
            return temp.toString();
        } else {
            if (isEllipsis) {

                StringBuffer temp = new StringBuffer(length);
                temp.append(str.substring(0, length - 3));
                temp.append("...");
                return temp.toString();
            } else {
                return str.substring(0, length);
            }
        }
    }
}

Related

  1. alignRight(CharSequence cs, int width, char c)
  2. alignRight(final long number, final int length)
  3. alignRight(final Object str, final int size)
  4. alignRight(String data)
  5. alignRight(String str, int size, char padChar)
  6. alignRight(String substring, int totalWidth, char fill)
  7. alignRight(String text, int length)
  8. alignRight(String val, char pad, int width)