Java String align left

Description

Java String align left

/**//w w w  . j ava 2 s  . c  o  m
 *
 * Copyright (c) 2006-2015, Speedment, Inc. All Rights Reserved.
 *
 * 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.
 */
//package com.demo2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String substring = "demo2s.com";
        int totalWidth = 20;
        System.out.println(">"+alignLeft(substring, totalWidth)+"<");
    }

    /**
     * Returns a string of the specified length where the substring is located
     * to the left, padded spaces on the right. If the specified 
     * {@code totalWidth} is less than the length of the substring, the
     * substring is returned but with the overflowing characters removed.
     * 
     * @param substring   the substring to align
     * @param totalWidth  the width of the returned string
     * @return            the padded string
     */
    public static String alignLeft(String substring, int totalWidth) {
        return alignLeft(substring, totalWidth, ' ');
    }

    /**
     * Returns a string of the specified length where the substring is located
     * to the left, padded with a character on the right. If the specified 
     * {@code totalWidth} is less than the length of the substring, the
     * substring is returned but with the overflowing characters removed.
     * 
     * @param substring   the substring to align
     * @param totalWidth  the width of the returned string
     * @param fill        the character to use for padding
     * @return            the padded string
     */
    public static String alignLeft(String substring, int totalWidth, char fill) {
        if (substring.length() > totalWidth) {
            return substring.substring(0, totalWidth);
        } else {
            return substring + repeat("" + fill, totalWidth - substring.length());
        }
    }

    /**
     * Repeats the specified substring a number of times.
     *
     * @param str    the string to repeat
     * @param count  the number of times to repeat it
     * @return       the new string
     */
    public static String repeat(String str, int count) {
        final StringBuilder result = new StringBuilder(str.length() * count);

        for (int i = 0; i < count; i++) {
            result.append(str);
        }

        return result.toString();
    }
}

//package com.demo2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String text = "demo2s.com";
        int width = 30;
        System.out.println(">"+alignLeft(text, width)+"<");
    }/*from www.  j  a  v  a  2 s  . c  o  m*/

    /**
     * Create left-aligned text with a maximum <code>width</code> characters.
     *
     * @param text the text that will be aligned.
     * @param width maximum number of characters.  Text exceeds this limit will be truncated.
     * @return aligned text.
     */
    public static String alignLeft(String text, int width) {
        if (text.length() < width) {
            StringBuilder tmp = new StringBuilder(text);
            int numOfSpaces = width - text.length() + 1;
            for (int i = 1; i < numOfSpaces; i++) {
                tmp.append(' ');
            }
            return tmp.toString();
        } else if (text.length() > width) {
            return text.substring(0, width);
        }
        return text;
    }
}

//package com.demo2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String str = "demo2s.com";
        int iAlignment = 1;
        int iLength = 20;
        System.out.println(">"+align(str, iAlignment, iLength)+"<");
    }/*from   w w w .  ja  v a2  s .co  m*/

    public static String align(String str, int iAlignment, int iLength, char c) {
        if (str == null) {
            return null;
        }
        if (str.length() > iLength) {
            return str.substring(0, iLength);
        }
        StringBuilder buf = new StringBuilder();
        if (iAlignment == 0) {
            str = lpad(str, str.length() + (iLength - str.length()) / 2, c);
            return rpad(str, iLength, c);
        }
        if (iAlignment == 2) {
            return lpad(str, iLength, c);
        }
        if (iAlignment == 1) {
            return rpad(str, iLength, c);
        } else {
            return buf.toString();
        }
    }

    public static String align(String str, int iAlignment, int iLength) {
        return align(str, iAlignment, iLength, ' ');
    }

    public static String lpad(String str, int iLength) {
        return lpad(str, iLength, ' ');
    }

    public static String lpad(String str, int iLength, char c) {
        if (str == null) {
            return null;
        }
        int iCount = iLength - str.length();
        if (iCount > 0) {
            return createMonoString(c, iCount) + str;
        } else {
            return str;
        }
    }

    public static String rpad(String str, int iLength) {
        return rpad(str, iLength, ' ');
    }

    public static String rpad(String str, int iLength, char c) {
        if (str == null) {
            return null;
        }
        int iCount = iLength - str.length();
        if (iCount > 0) {
            return str + createMonoString(c, iCount);
        } else {
            return str;
        }
    }

    public static String createMonoString(char c, int iLength) {
        StringBuilder buf = new StringBuilder();
        for (int iIndex = 0; iIndex < iLength; iIndex++) {
            buf.append(c);
        }

        return buf.toString();
    }
}



PreviousNext

Related