Java String align center
public class Main { public static void main(String[] argv) throws Exception { String str = "demo2s.com"; int size = 30; System.out.println(">" + center(str, size) + "<"); }/* ww w.j a va 2 s.co m*/ private static final String[] PADDING = new String[Character.MAX_VALUE]; private static final int PAD_LIMIT = 8192; public static String center(String str, int size) { return center(str, size, ' '); } public static String center(String str, int size, char padChar) { if (str == null || size <= 0) { return str; } int strLen = str.length(); int pads = size - strLen; if (pads <= 0) { return str; } str = leftPad(str, strLen + pads / 2, padChar); str = rightPad(str, size, padChar); return str; } public static String center(String str, int size, String padStr) { if (str == null || size <= 0) { return str; } if (padStr == null || padStr.length() == 0) { padStr = " "; } int strLen = str.length(); int pads = size - strLen; if (pads <= 0) { return str; } str = leftPad(str, strLen + pads / 2, padStr); str = rightPad(str, size, padStr); return str; } public static String leftPad(String str, int size) { return leftPad(str, size, ' '); } public static String leftPad(String str, int size, char padChar) { if (str == null) { return null; } int pads = size - str.length(); if (pads <= 0) { return str; // returns original String when possible } if (pads > PAD_LIMIT) { return leftPad(str, size, String.valueOf(padChar)); } return padding(pads, padChar).concat(str); } public static String leftPad(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; // returns original String when possible } if (padLen == 1 && pads <= PAD_LIMIT) { return leftPad(str, size, padStr.charAt(0)); } 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); } } public static String rightPad(String str, int size) { return rightPad(str, size, ' '); } public static String rightPad(String str, int size, char padChar) { if (str == null) { return null; } int pads = size - str.length(); if (pads <= 0) { return str; // returns original String when possible } if (pads > PAD_LIMIT) { return rightPad(str, size, String.valueOf(padChar)); } return str.concat(padding(pads, padChar)); } public static String rightPad(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; // returns original String when possible } if (padLen == 1 && pads <= PAD_LIMIT) { return rightPad(str, size, padStr.charAt(0)); } if (pads == padLen) { return str.concat(padStr); } else if (pads < padLen) { return str.concat(padStr.substring(0, pads)); } else { char[] padding = new char[pads]; char[] padChars = padStr.toCharArray(); for (int i = 0; i < pads; i++) { padding[i] = padChars[i % padLen]; } return str.concat(new String(padding)); } } private static String padding(int repeat, char padChar) { String pad = PADDING[padChar]; if (pad == null) { pad = String.valueOf(padChar); } while (pad.length() < repeat) { pad = pad.concat(pad); } PADDING[padChar] = pad; return pad.substring(0, repeat); } }
//package com.demo2s; public class Main { public static void main(String[] argv) throws Exception { String sLine = "demo2s.com"; System.out.println(">"+alignCenter(sLine,30)+"<"); }/* www . j a v a 2s . c o m*/ public static String alignCenter(String sLine, int iSize) { if (sLine.length() > iSize) { return alignRight(sLine.substring(0, (sLine.length() + iSize) / 2), iSize); } else { return alignRight(sLine + getWhiteString((iSize - sLine.length()) / 2), iSize); } } public static String alignCenter(String sLine) { return alignCenter(sLine, 42); } public static String alignRight(String sLine, int iSize) { if (sLine.length() > iSize) { return sLine.substring(sLine.length() - iSize); } else { return getWhiteString(iSize - sLine.length()) + sLine; } } public static String getWhiteString(int iSize, char cWhiteChar) { char[] cFill = new char[iSize]; for (int i = 0; i < iSize; i++) { cFill[i] = cWhiteChar; } return new String(cFill); } public static String getWhiteString(int iSize) { return getWhiteString(iSize, ' '); } }
//package com.demo2s; public class Main { public static void main(String[] argv) throws Exception { String substring = "demo2s.com"; int totalWidth = 30; System.out.println(">"+alignCenter(substring, totalWidth)+"<"); }// ww w . j a v a 2s .c o m /** * Returns a string of the specified length where the substring is located * in the middle, padded with spaces on both sides. If uneven, an extra * space will be added to the right side. 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 alignCenter(String substring, int totalWidth) { return alignCenter(substring, totalWidth, ' '); } /** * Returns a string of the specified length where the substring is located * in the middle, padded with a character on both sides. If uneven, an extra * space will be added to the right side. 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 alignCenter(String substring, int totalWidth, char fill) { if (substring.length() > totalWidth) { return substring.substring(0, totalWidth); } else { final double padding = (totalWidth - substring.length()) / 2d; final int left = (int) Math.floor(padding); final int right = (int) Math.ceil(padding); return repeat("" + fill, left) + substring + repeat("" + fill, right); } } /** * 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(); } } /** * * 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 text = "demo2s.com"; int width = 30; System.out.println(">"+alignCenter(text, width)+"<"); }/* w ww . j av a 2s.c o m*/ /** * Create center-aligned text with a maximum <code>width</code> characters. * * @param text the text that will bealigned. * @param width maximum number of characters. Text exceeds this limit will be truncated. * @return aligned text. */ public static String alignCenter(String text, int width) { if (text.length() < width) { StringBuilder tmp = new StringBuilder(); int numOfSpaces = (width - text.length()) / 2; for (int i = 0; i < numOfSpaces; i++) { tmp.append(' '); } tmp.append(text); while (tmp.length() < width) { tmp.append(' '); } return tmp.toString(); } else if (text.length() > width) { return text.substring(0, width); } return text; } }