de.invation.code.toval.misc.StringUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of TOVAL Show documentation
Show all versions of TOVAL Show documentation
TOVAL comprises a set of java classes for common programming issues. It includes utils for arrays, lists, sets and collections for convenient handling and modification, but also support for mathematic definitions concerning logic (clauses + resolution) together with some algorithms for permutations, powersets and resolution. Additionally it contains a number of types for multisets, matrices with object keys and much more.
The newest version!
package de.invation.code.toval.misc;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import de.invation.code.toval.validate.ParameterException;
import de.invation.code.toval.validate.Validate;
public class StringUtils {
public static StringTokenizer splitArrayString(String array, String delimiter){
array = array.replace("[", "");
array = array.replace("]", "");
return new StringTokenizer(array, delimiter);
}
/**
* Takes a string and splits it according to the quote character '.
* Use this method for splitting a string into several parts, where each part corresponds to a quoted substring in the input.
* A string "['stringA' 'stringB']" will result in the list (stringA,stringB) with ' being the character used for qouting.
* @param array Input string, containing quoted character sets.
* @param quoteChar The character used to quote strings in the input string.
* @return list, containing all quotes strings in the input string.
*/
public static List splitArrayStringQuoted(String array, char quoteChar){
array = array.replace("[", "");
array = array.replace("]", "");
List result = new ArrayList<>();
Integer actStart = null;
for(int i=0; i
* Use this method for splitting a string into several parts, where each part corresponds to a quoted substring in the input.
* A string "['stringA' 'stringB']" will result in the list (stringA,stringB) with ' being the character used for qouting.
* @param array Input string, containing quoted character sets.
* @return list, containing all quotes strings in the input string.
*/
public static List splitArrayStringQuoted(String array){
return splitArrayStringQuoted(array, '\'');
}
public static int countOccurrences(String string, char character){
int count = 0;
for(char c: string.toCharArray()){
if(c == character)
count++;
}
return count;
}
public static String removeLeading(String string, char character){
int index = 0;
for(int i=0; i 0)
return "";
return string;
}
public static String removeEnding(String string, char character){
int index = string.length()-1;
for(int i=string.length()-1; i>=0; i--){
if(string.charAt(i) != character){
return string.substring(0, index+1);
}
index--;
}
if(index < string.length()-1)
return "";
return string;
}
public static String removeSurrounding(String string, char character){
String result = removeLeading(string, character);
return removeEnding(result, character);
}
public static String createString(char character, int occurrences){
char[] arr = new char[occurrences];
for(int i=0; i")+"