/** * checks if all of the Strings has text (NOT null, zero length or only whitespace) * * @param strings arbitrry number of Strings. * @return true if ALL strings contain text. */ public static boolean allHasText(String... strings) { checkVarargString(strings); for (String s : strings) { if (!hasText(s)) { return false; } } return true; }
public boolean canPass(String str) { return !Strings.isNullOrEmpty(str); } }
/** * <p>Left pad a String with spaces (' ').</p> * <p/> * <p>The String is padded to the size of <code>size<code>.</p> * <p/> * <pre> * StringUtils.leftPad(null, *) = null * StringUtils.leftPad("", 3) = " " * StringUtils.leftPad("bat", 3) = "bat" * StringUtils.leftPad("bat", 5) = " bat" * StringUtils.leftPad("bat", 1) = "bat" * StringUtils.leftPad("bat", -1) = "bat" * </pre> * * @param str the String to pad out, may be null * @param size the size to pad to * @return left padded String or original String if no padding is necessary, <code>null</code> if * null String input */ public static String leftPad(String str, int size) { return leftPad(str, size, " "); }
/** * checks if all of the Strings are empty (null or zero length) * * @param strings arbitrry number of Strings. * @return true if all Strings are empty */ public static boolean allNullOrEmpty(String... strings) { checkVarargString(strings); for (String s : strings) { if (!isNullOrEmpty(s)) { return false; } } return true; }
public Map<String, String> loadFromFile(SimpleTextReader sfr) throws IOException { List<String> lines = sfr.asStringList(); if (lines.size() == 0) { return Collections.emptyMap(); } Map<String, String> result = new LinkedHashMap<>(); for (String line : lines) { if (!line.contains(separator)) { throw new IllegalArgumentException("line: [" + line + "] has no separator:" + separator); } String key = Strings.subStringUntilFirst(line, separator).trim(); String value = Strings.subStringAfterFirst(line, separator).trim(); result.put(key, value); } return result; } }
@Override public boolean processLine(String s) { s = s.trim(); String first = Strings.subStringUntilFirst(s, " "); if (ignoreLines.contains(first)) { return true; } if (first.equalsIgnoreCase(END_SENTENCE_TAG)) { sentences.add(new SentenceData(currentWords)); currentWords = Lists.newArrayList(); } else { currentWords.add(new WordData(s)); } return true; }
private static void listApplications(List<ConsoleApp> apps) { System.out.println("List of available applications:"); System.out.println("==============================="); for (ConsoleApp app : apps) { String simpleName = app.getClass().getSimpleName(); System.out.println(simpleName); System.out.println(Strings.repeat("-", simpleName.length())); String wrapped = wrap(app.description(), 80); System.out.println(wrapped); System.out.println(); } System.exit(0); }
/** * inserts the <code>stringToInsert</code> with given <code>interval</code> starting from right. * </p> * <pre>("0123456", 2, "-") returns "0-12-34-56"</pre> * * @param str input string * @param interval : interval amount * @param stringToInsert : character to insert. * @return the formatted string. null, if <code>str</code> is null * @throws IllegalArgumentException if <code>interval</code> is negative */ public static String insertFromRight(String str, int interval, String stringToInsert) { if (interval < 0) { throw new IllegalArgumentException("interval value cannot be negative."); } if (str == null || interval == 0 || interval >= str.length() || isNullOrEmpty(stringToInsert)) { return str; } StringBuilder b = new StringBuilder(); int j = 0; for (int i = str.length() - 1; i >= 0; i--) { b.append(str.charAt(i)); j++; if (j % interval == 0 && j <= str.length() - 1) { b.append(stringToInsert); } } return reverse(b.toString()); }
return null; if (isNullOrEmpty(padStr)) { padStr = " "; return rightPad(str, size, padStr.charAt(0));
public boolean canPass(String str) { return Strings.hasText(str); } }
static void processCorpus(Path in, Path out) throws IOException { BlockTextLoader loader = BlockTextLoader.fromPath(in, 10000); try (PrintWriter pw = new PrintWriter(out.toFile(), "utf-8")) { for (TextChunk chunk : loader) { LinkedHashSet<String> unique = new LinkedHashSet<>(chunk.getData()); for (String l : unique) { if (!Strings.containsNone(l, "[]#~|")) { continue; } l = l.toLowerCase(es).replaceAll("[^0-9a-zñáéíóúü]", " ") .replaceAll("\\s+", " ").trim(); if (l.length() == 0) { continue; } if (l.length() < 20) { continue; } pw.println(l); } } } }
public WordParse(String parseString) { if (!parseString.contains("+")) { root = parseString; igs = Lists.newArrayList(); allIgs = ""; } else { if (parseString.startsWith("+")) { root = "+"; igs = Lists.newArrayList("+Punc"); allIgs = "+Punc"; } else { root = Strings.subStringUntilFirst(parseString, "+"); String rest = "+" + Strings.subStringAfterFirst(parseString, "+"); String igsStr = rest.replaceAll("\\^DB", " "); igs = Splitter.on(" ").omitEmptyStrings().trimResults().splitToList(igsStr); allIgs = rest.replaceAll("\\^DB", " "); } } all = parseString; }
public static String convertOrdinalNumberString(String input) { String numberPart = input; if (input.endsWith(".")) { numberPart = Strings.subStringUntilFirst(input, "."); } long number = Long.parseLong(numberPart); String text = convertToString(number); String[] words = text.trim().split("[ ]+"); String lastNumber = words[words.length - 1]; if (ordinalMap.containsKey(lastNumber)) { lastNumber = ordinalMap.get(lastNumber); } else { throw new RuntimeException("Cannot find ordinal reading for:" + lastNumber); } StringBuilder sb = new StringBuilder(); for (int i = 0; i < words.length - 1; i++) { sb.append(words[i]).append(" "); } sb.append(lastNumber); return sb.toString(); }
public static Weights loadFromLines(List<String> lines) { FloatValueMap<String> data = new FloatValueMap<>(10000); for (String s : lines) { float weight = Float.parseFloat(Strings.subStringUntilFirst(s, " ")); String key = Strings.subStringAfterFirst(s, " "); data.set(key, weight); } return new Weights(data); }
LineData(String line) { this.word = Strings.subStringUntilFirst(line, " "); if (word.length() == 0) { throw new LexiconException("Line " + line + " has no word data!"); } this.metaData = readMetadata(line); }
public KeyValueReader(String seperator, String ignorePrefix) { if (Strings.isNullOrEmpty(seperator)) { throw new IllegalArgumentException("Separator is null or empty!"); } this.separator = seperator; this.ignorePrefix = ignorePrefix; }
/** * Returns a Buffered reader for the given input stream and charset. if charset is UTF-8 it * explicitly checks for UTF-8 BOM information. * * @param is input stream for the reader. * @param charset charset string, if null,empty or has only whitespace, system uses default * encoding. * @return BufferedReader * @throws java.io.IOException: if the given encoding is not supported, or an error occurs if * given charset is utf-8 and an IO error occurs during utf-8 BOM detection operation. */ public static BufferedReader getReader(InputStream is, String charset) throws IOException { checkNotNull(is, "input stream cannot be null"); if (!Strings.hasText(charset)) { return getReader(is); } if (charset.trim().equalsIgnoreCase("utf-8")) { return new BufferedReader(new InputStreamReader(forceUTF8(is), "utf-8"), CHAR_BUFFER_SIZE); } else { return new BufferedReader(new InputStreamReader(is, charset), CHAR_BUFFER_SIZE); } }