Created
June 14, 2012 06:22
-
-
Save colin-haber/2928314 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment