/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.intuism.core.utility; import com.intuism.core.exceptional.exception.MissingSubstringException; import com.intuism.core.exceptional.guardian.Guardian; import java.util.*; /** * TODO relocate to base underneath exceptional and semadoc * @author randahl */ public class StringUtility { public static List toStringSort(Collection objects) { List sorted = new ArrayList(objects); Collections.sort( sorted, new Comparator() { @Override public int compare(Object objectA, Object objectB) { String aString = objectA.toString(); String bString = objectB.toString(); return aString.compareTo(bString); } } ); return sorted; } public static String prependMultiple(String string, String prependix, int count) { StringBuilder builder = new StringBuilder(); for(int i = 0; i < count; i++) { builder.append(prependix); } builder.append(string); return builder.toString(); } public static String capitalizeFirstLetter(String string) { // Guardian.notNull("computerName", string); if(string.length() > 0) { string = string.substring(0, 1).toUpperCase() + string.substring(1); } return string; } public static String decapitalizeFirstLetter(String string) { // Guardian.notNull("computerName", string); if(string.length() > 0) { string = string.substring(0, 1).toLowerCase() + string.substring(1); } return string; } public static boolean contains(String string, String substring, int exactCount) { int i = 0; boolean contained = true; while(contained && exactCount > 0) { i = string.indexOf(substring, i); if(i == -1) contained = false; else { exactCount--; if(exactCount > 0) { i += substring.length(); if(i == string.length()) contained = false; } } } return contained; } public static String remove(String string, String startTag, String endTag) { int startIndex = string.indexOf(startTag); if(startIndex == -1) throw new MissingSubstringException("string", startTag); int endIndex = string.indexOf(endTag); if(endIndex == -1) throw new MissingSubstringException("string", endTag); endIndex += endTag.length(); return string.substring(0, startIndex) + string.substring(endIndex); } public static String extract(String string, String startMark, String endMark) { int startMarkIndex = string.indexOf(startMark); String extraction = null; if(startMarkIndex != -1) { int startIndex = startMarkIndex + startMark.length(); int endIndex = string.indexOf(endMark, startIndex); if(endIndex != -1) { extraction = string.substring( startMarkIndex + startMark.length(), endIndex ); } } return extraction; } public static String unfold(Collection objects) { return unfold(objects, ", "); } public static String unfold(Collection objects, String separator) { StringBuilder builder = new StringBuilder(); for(Object object : objects) { if(builder.length() > 0) builder.append(separator); if(object != null) builder.append(object.toString()); else builder.append("null"); } return builder.toString(); } public static List separate(String string, String delimiters) { List separated = new ArrayList(); StringTokenizer tokenizer = new StringTokenizer(string, delimiters); while(tokenizer.hasMoreTokens()) separated.add(tokenizer.nextToken()); return separated; } /** * Removes all occurrences of the specified unwanted substrings from the * specified string * * @param string The string to sift * @param unwantedSubstrings The substrings to remove from the input string * @return a sifted string where all unwanted strings have been removed */ public static String sift(String string, String... unwantedSubstrings) { for(String unwantedSubstring : unwantedSubstrings) { string = string.replace(unwantedSubstring, ""); } return string; } public static int countDigits(String string) { int count = 0; for(int i = 0; i < string.length(); i++) { if(Character.isDigit(string.charAt(i))) count++; } return count; } public static Character containsNonDigit(String string) { return containsAnyBut(string, '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'); } public static Character containsAnyBut(String string, Character... allowed) { Character disallowed = null; int i = 0; while(i < string.length() && disallowed == null) { Character c = string.charAt(i); if(!ArrayUtility.contains(allowed, c)) { disallowed = c; } i++; } return disallowed; } public static String lastWord(String string) { String lastWord = null; if(string != null && string.length() > 0) { string = string.trim(); if(string.length() == 1) lastWord = string; else { boolean endLetter = Character.isLetterOrDigit(string.charAt(string.length() - 1)); int index = string.length() - 2; boolean separationFound = false; while(!separationFound && index >= 0) { boolean letter = Character.isLetterOrDigit(string.charAt(index)); if(letter != endLetter) separationFound = true; else index--; } int start = index + 1; lastWord = string.substring(start); } } return lastWord; } public static boolean isInteger(String string) { boolean isInteger = false; if(string != null && !string.isEmpty()) { isInteger = true; int i = 0; while(isInteger && i < string.length()) { isInteger = Character.isDigit(string.charAt(i)); i++; } } return isInteger; } public static String clearWhitespace(String text) { Guardian.notNull("text", text); StringBuilder builder = new StringBuilder(); for(int i = 0; i < text.length(); i++) { char c = text.charAt(i); if(!Character.isWhitespace(c)) builder.append(c); } return builder.toString(); } }