ai.libs.jaicore.basic.StringUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jaicore-basic Show documentation
Show all versions of jaicore-basic Show documentation
Fundamental utils required by many other starlibs projects.
package ai.libs.jaicore.basic;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Random;
import java.util.Set;
import java.util.stream.IntStream;
/**
* This class provides handy utility functions when dealing with Strings.
*
* @author fmohr, mwever
*
*/
public class StringUtil {
private StringUtil() {
// prevent instantiation of this util class.
}
/**
* Getter for all available common characters of the system. Digits can be included if desired.
*
* @param includeDigits Flag whether to include digits in the array of the system's common characters.
* @return An array of the system's common characters.
*/
public static char[] getCommonChars(final boolean includeDigits) {
/* create char array */
List chars = new LinkedList<>();
for (int i = 65; i <= 90; i++) {
chars.add((char) i);
}
for (int i = 97; i <= 122; i++) {
chars.add((char) i);
}
if (includeDigits) {
for (int i = 48; i <= 57; i++) {
chars.add((char) i);
}
}
char[] charsAsArray = new char[chars.size()];
for (int i = 0; i < charsAsArray.length; i++) {
charsAsArray[i] = chars.get(i);
}
return charsAsArray;
}
/**
* Returns a random string of a desired length and from a given set of characters.
*
* @param length The length of the resulting random string.
* @param chars The set of characters to be used to generate a random string.
* @return The generated random string.
*/
public static String getRandomString(final int length, final char[] chars, final long seed) {
StringBuilder s = new StringBuilder();
Random rand = new Random(seed);
for (int i = 0; i < length; i++) {
s.append(chars[rand.nextInt(chars.length)]);
}
return s.toString();
}
/**
* Concatenates the string representations of an array of objects using ", " as a separator.
* @param array The array of objects of which the string representation is to be concatenated.
* @return The concatenated string of the given objects' string representation.
*/
public static String implode(final Object[] array) {
return implode(array, ", ");
}
/**
* Concatenates the string representations of a set of objects using ", " as a separator.
* @param set The set of objects of which the string representation is to be concatenated.
* @return The concatenated string of the given objects' string representation.
*/
public static String implode(final Set
© 2015 - 2024 Weber Informatics LLC | Privacy Policy