Skip to content

Instantly share code, notes, and snippets.

@robbypelssers
Last active December 19, 2015 09:49
StringUtils (Java)
import static java.lang.Math.pow;
public class JavaStringUtils {
/**
*
* @param source
* @param c
* @param interval
* @return a new string with char c appended after each interval of characters
*/
public static String interleave(String source, char c, int interval) {
return
source.length() < interval + 1
? source
: source.substring(0, interval).concat(String.valueOf(c))
.concat(interleave(source.substring(interval), c, interval));
}
/**
*
* @param source
* @return a Double representation of a binary string
*/
public static Double binaryString2Double(String source) {
Double result = 0D;
int length = source.length();
for (int index = length - 1; index > -1; index--) {
int numAtIndex = Integer.valueOf(String.valueOf(source.charAt(index)));
result = result + numAtIndex * pow(2, index);
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment