Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.n1nja.utils;
- /**
- * Useful classes for manipulating {@link java.lang.String String}s.
- * @author Colin Haber
- * @version 0.0.0
- */
- public class Strings {
- /**
- * The default {@code char} used for padding
- * {@link java.lang.String String}s.
- * @since 0.0.0
- * @see #pad(String, int, String, boolean)
- */
- private static char DEFAULT_PAD_CHAR = ' ';
- /**
- * The default side to pad from.
- * @since 0.0.0
- * @see #pad(String, int, String, boolean)
- */
- private static boolean DEFAULT_PAD_SIDE = Strings.PAD_LEFT;
- /**
- * Indicates padding should be done from the left.
- * @since 0.0.0
- * @see #pad(String, int, String, boolean)
- */
- public static final boolean PAD_LEFT = true;
- /**
- * Indicates padding should be done from the right.
- * @since 0.0.0
- * @see #pad(String, int, String, boolean)
- */
- public static final boolean PAD_RIGHT = false;
- /**
- * Pads a {@link java.lang.String String} from the left with spaces.
- * @since 0.0.0
- * @param s the {@link java.lang.String String} to pad
- * @param len the amount of spaces to pad with
- * @return the padded {@link java.lang.String String}
- */
- public static String pad(String s, int len) {
- return Strings.pad(s, len, Strings.DEFAULT_PAD_CHAR);
- }
- /**
- * Pads a {@link java.lang.String String} from the left with the provided
- * {@code char}.
- * @since 0.0.0
- * @param s the {@link java.lang.String String} to pad
- * @param len the amount of {@code char}s to pad with
- * @param pad the {@code char} to pad with
- * @return the padded {@link java.lang.String String}
- */
- public static String pad(String s, int len, char pad) {
- return Strings.pad(s, len, Character.toString(pad));
- }
- /**
- * Pads a {@link java.lang.String String} from the left with the provided
- * {@code char}.
- * @since 0.0.0
- * @param s the {@link java.lang.String String} to pad
- * @param len the amount of {@code char}s to pad with
- * @param pad the {@link java.lang.String String} to pad with
- * @return the padded {@link java.lang.String String}
- */
- public static String pad(String s, int len, String pad) {
- return Strings.pad(s, len, pad, Strings.DEFAULT_PAD_SIDE);
- }
- /**
- * Pads a {@link java.lang.String String} from the left with the provided
- * {@code char}.
- * @since 0.0.0
- * @param s the {@link java.lang.String String} to pad
- * @param len the amount of {@code char}s to pad with
- * @param side the side to pad from
- * @return the padded {@link java.lang.String String}
- */
- public static String pad(String s, int len, boolean side) {
- return Strings.pad(s, len, Strings.DEFAULT_PAD_CHAR, side);
- }
- /**
- * Pads a {@link java.lang.String String} from the left with the provided
- * {@code char}.
- * @since 0.0.0
- * @param s the {@link java.lang.String String} to pad
- * @param len the amount of {@code char}s to pad with
- * @param pad the {@code char} to pad with
- * @param side the side to pad from
- * @return the padded {@link java.lang.String String}
- */
- public static String pad(String s, int len, char pad, boolean side) {
- return Strings.pad(s, len, Character.toString(pad), side);
- }
- /**
- * Pads a {@link java.lang.String String} from the left with the provided
- * {@code char}.
- * @since 0.0.0
- * @param s the {@link java.lang.String String} to pad
- * @param len the amount of {@code char}s to pad with
- * @param pad the {@link java.lang.String String} to pad with
- * @param side the side to pad from
- * @return the padded {@link java.lang.String String}
- */
- public static String pad(String s, int len, String pad, boolean side) {
- StringBuilder padder = new StringBuilder(Math.max(s.length(), len));
- padder.append(s);
- if (side) {
- while (padder.length() < len) {
- padder.insert(0, pad);
- }
- } else {
- while (padder.length() < len) {
- padder.append(pad);
- }
- }
- return padder.toString();
- }
- }
Add Comment
Please, Sign In to add comment