centerize String - Android java.lang

Android examples for java.lang:String Format

Description

centerize String

Demo Code


//package com.java2s;

public class Main {
    private static String centerizeString(String str, int maxLength) {
        if (str.length() > maxLength) {
            return str;
        } else {//from ww w . j a v a  2s  .  co  m
            int padLength = (maxLength - str.length()) / 2;
            String result = "";
            for (int i = 0; i < padLength; i++) {
                result += " ";
            }
            result += str;
            for (int i = 0; i < padLength; i++) {
                result += " ";
            }

            // parity
            if (str.length() % 2 == 0)
                result += " ";

            return result;
        }

    }
}

Related Tutorials