Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE

public class Main {
    /**
     * Converts the given string to a char-array of exactly the given length.
     * If the given string is short than the wanted length, then the array is
     * padded with trailing padding chars. If the string is longer, the last
     * character are cut off that the string has the wanted size.
     *
     * @param string The string to convert.
     * @param exactArrayLength The length of the retirned char-array.
     * @param paddingChar The character to use for padding, if necessary.
     * @return The string as char array, padded or cut off, if necessary.
     *         The array will have length exactArrayLength. null, if the
     *         given string is null.
     * @preconditions (exactArrayLength >= 0)
     * @postconditions (result == null)
     *                 or (result <> null)
     *                    and (result.length == exactArrayLength)
     */
    public static char[] toPaddedCharArray(String string, int exactArrayLength, char paddingChar) {
        char[] charArray = null;

        if (string != null) {
            int stringLength = string.length();
            charArray = new char[exactArrayLength];
            string.getChars(0, Math.min(stringLength, exactArrayLength), charArray, 0);
            for (int i = stringLength; i < charArray.length; i++) { // fill the rest of the array with padding char
                charArray[i] = paddingChar;
            }
        }

        return charArray;
    }
}