Created
June 23, 2012 05:27
-
-
Save colin-haber/2977014 to your computer and use it in GitHub Desktop.
A nascent collection of String utilities.
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_START; | |
/** | |
* Indicates padding should be done from the left. | |
* @since 0.0.0 | |
* @see #pad(String, int, String, boolean) | |
*/ | |
public static final boolean PAD_START = true; | |
/** | |
* Indicates padding should be done from the right. | |
* @since 0.0.0 | |
* @see #pad(String, int, String, boolean) | |
*/ | |
public static final boolean PAD_END = 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) { | |
if (len < 0) { | |
throw new IllegalArgumentException("Cannot pad negative lengths."); | |
} else if (s.length() <= 0 || pad.length() <= 0) { | |
throw new IllegalArgumentException("Cannot have zero-length Strings."); | |
} | |
StringBuilder padder = new StringBuilder(Math.max(s.length(), len)); | |
int padlen = len - s.length(); | |
if (side == Strings.PAD_START) { | |
padder.append(s); | |
padlen = len; | |
} | |
char[] padChars = pad.toCharArray(); | |
int i = 0; | |
while (padder.length() < padlen) { | |
padder.append(padChars[i]); | |
i++; | |
if (i >= padChars.length) { | |
i = 0; | |
} | |
} | |
if (side == Strings.PAD_END) { | |
padder.append(s); | |
} | |
return padder.toString(); | |
} | |
/** | |
* Repeats a {@link java.lang.String String} until it reaches length | |
* {@code len}. For example, {@code repeat("tittysprinkles", 20)} would | |
* return {@code "tittysprinklestittys"}. | |
* @param s the {@link java.lang.String String} to be repeated | |
* @param len the target length | |
* @return a {@link java.lang.String String} length {@code len}, containing | |
* {@code s} repeated until the target length. | |
*/ | |
public static String repeat(String s, int len) { | |
if (len < 0) { | |
throw new IllegalArgumentException("Cannot repeat negative lengths."); | |
} else if (s.length() <= 0) { | |
throw new IllegalArgumentException("Cannot have zero-length Strings."); | |
} | |
StringBuilder repeater = new StringBuilder(len); | |
do { | |
repeater.append(len - repeater.length() > s.length() ? s : s.substring(0, len - repeater.length())); | |
} while (repeater.length() < len); | |
return repeater.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment