List of usage examples for java.text BreakIterator setText
public abstract void setText(CharacterIterator newText);
From source file:com.arthackday.killerapp.util.Util.java
public static String truncate(String text, int charLimit) { if (text.length() > charLimit) { BreakIterator bi = BreakIterator.getWordInstance(); bi.setText(text); int cutOff = bi.following(charLimit); text = text.substring(0, cutOff) + " ..."; }//from w ww . j a va 2s .c o m return text; }
From source file:org.yamj.core.tools.StringTools.java
/** * Check that the passed string is not longer than the required length and trim it if necessary * * @param sourceString The string to check * @param requiredLength The required length (Maximum) * @param trimToWord Trim the source string to the last space to avoid partial words * @param endingSuffix The ending to append if the string is longer than the required length * @return//from w w w . j a v a2 s.com */ public static String trimToLength(String sourceString, int requiredLength, boolean trimToWord, String endingSuffix) { String changedString = sourceString.trim(); if (StringUtils.isNotBlank(changedString)) { if (changedString.length() <= requiredLength) { // No need to do anything return changedString; } else { if (trimToWord) { BreakIterator bi = BreakIterator.getWordInstance(); bi.setText(changedString); int biLength = bi.preceding(requiredLength - endingSuffix.length()); return changedString.substring(0, biLength).trim() + endingSuffix; } else { // We know that the source string is longer that the required length, so trim it to size return changedString.substring(0, requiredLength - endingSuffix.length()).trim() + endingSuffix; } } } return changedString; }
From source file:eu.fbk.utils.lsa.util.Anvur.java
static String tokenize(String in) { //print each word in order BreakIterator boundary = BreakIterator.getWordInstance(); boundary.setText(in); StringBuilder out = new StringBuilder(); int start = boundary.first(); for (int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary.next()) { out.append(" "); out.append(in.substring(start, end)); }//from w ww . ja v a 2 s. co m return out.toString(); }
From source file:com.conversantmedia.mapreduce.tool.RunJob.java
private static void splitLine(List<String> lines, String text, int maxLength) { BreakIterator boundary = BreakIterator.getLineInstance(); boundary.setText(text); int start = boundary.first(); int end = boundary.next(); int lineLength = 0; StringBuilder buffer = new StringBuilder(); while (end != BreakIterator.DONE) { String word = text.substring(start, end); lineLength = lineLength + word.length(); if (lineLength > maxLength) { lineLength = word.length();//from w ww. j a va2 s.com lines.add(buffer.toString()); buffer.setLength(0); } buffer.append(word); start = end; end = boundary.next(); } lines.add(buffer.toString()); }
From source file:com.moviejukebox.tools.StringTools.java
/** * Check that the passed string is not longer than the required length and * trim it if necessary// w w w . j av a2 s . c o m * * @param sourceString The string to check * @param requiredLength The required length (Maximum) * @param trimToWord Trim the source string to the last space to avoid * partial words * @param endingSuffix The ending to append if the string is longer than the * required length * @return */ public static String trimToLength(String sourceString, int requiredLength, boolean trimToWord, String endingSuffix) { String changedString = sourceString.trim(); if (isValidString(changedString)) { if (changedString.length() <= requiredLength) { // No need to do anything return changedString; } if (trimToWord) { BreakIterator bi = BreakIterator.getWordInstance(); bi.setText(changedString); int biLength = bi.preceding(requiredLength - endingSuffix.length() + 1); return changedString.substring(0, biLength).trim() + endingSuffix; } // We know that the source string is longer that the required length, so trim it to size return changedString.substring(0, requiredLength - endingSuffix.length()).trim() + endingSuffix; } return changedString; }
From source file:org.eclipse.fx.core.text.TextUtil.java
/** * Find the end offset of the word// www. j a v a 2s.c o m * * @param content * the content * @param offset * the offset to start the search from * @param pointAsBoundary * should the '.' treated as word boundary * @return the end offset or {@link BreakIterator#DONE} */ public static int findWordEndOffset(IterableCharSequence content, int offset, boolean pointAsBoundary) { BreakIterator wordInstance = BreakIterator.getWordInstance(); wordInstance.setText(content.getIterator()); int rv = wordInstance.following(offset); if (rv != BreakIterator.DONE && pointAsBoundary) { String s = content.subSequence(offset, rv).toString(); int idx = s.indexOf('.'); if (idx >= 0) { rv = offset + idx; } if (rv == offset) { rv = offset + 1; } } return rv; }
From source file:org.eclipse.fx.core.text.TextUtil.java
/** * Find the start offset of the word/*from w ww . j av a2 s .c om*/ * * @param content * the content * @param offset * the offset to start the search from * @param pointAsBoundary * should the '.' treated as word boundary * @return the start offset or or {@link BreakIterator#DONE} */ public static int findWordStartOffset(IterableCharSequence content, int offset, boolean pointAsBoundary) { BreakIterator wordInstance = BreakIterator.getWordInstance(); wordInstance.setText(content.getIterator()); int rv = wordInstance.preceding(offset); if (rv != BreakIterator.DONE && pointAsBoundary) { String s = content.subSequence(rv, offset).toString(); int idx = s.lastIndexOf('.'); if (idx > 0) { rv += idx + 1; } // move before the point if (rv == offset) { rv -= 1; } } return rv; }
From source file:org.eclipse.fx.core.text.TextUtil.java
/** * Find the bounds of the word// w ww . j a va2 s .c o m * * @param content * the content * @param offset * the offset * @param pointAsBoundary * should the '.' treated as word boundary * @return a tuple of value representing start and end */ public static IntTuple findWordBounds(IterableCharSequence content, int offset, boolean pointAsBoundary) { BreakIterator wordInstance = BreakIterator.getWordInstance(); wordInstance.setText(content.getIterator()); int previous = wordInstance.preceding(offset); int next = wordInstance.following(offset); if (pointAsBoundary && previous != BreakIterator.DONE && next != BreakIterator.DONE) { String preMatch = content.subSequence(previous, offset).toString(); String postMatch = content.subSequence(offset, next).toString(); int idx = preMatch.lastIndexOf('.'); if (idx > 0) { previous += idx + 1; } idx = postMatch.indexOf('.'); if (idx > 0) { next = offset + idx; } } return new IntTuple(previous, next); }
From source file:graphene.util.StringUtils.java
/** * Convert a string to a list of strings broken up by end of sentence * tokens./*from w w w .j av a 2s .c o m*/ * * @param input * @param locale * @return */ public static List<String> convertToSentences(final String input, final Locale locale) { final BreakIterator iterator = BreakIterator.getSentenceInstance(locale); iterator.setText(input); final ArrayList<String> sentences = new ArrayList<String>(); int start = iterator.first(); for (int end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator.next()) { sentences.add(input.substring(start, end)); } return sentences; }
From source file:org.lnicholls.galleon.util.Tools.java
public static String[] layout(int width, FontMetrics metrics, String text) { ArrayList lines = new ArrayList(); if (text != null) { String line = ""; BreakIterator boundary = BreakIterator.getWordInstance(); boundary.setText(text); int start = boundary.first(); for (int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary.next()) { String word = text.substring(start, end); String trimmed = word.replaceAll(" ", ""); int metricsWidth = (line + word).length() * 20; if (metrics != null) metricsWidth = metrics.stringWidth(line + word); if (trimmed.equals("\n") || trimmed.equals("\r") || trimmed.equals("\r\n")) { lines.add(line.trim());/*from ww w. j ava 2s . c o m*/ line = ""; } else if (metricsWidth > width) { lines.add(line.trim()); line = word; } else line = line + word; } if (line.trim().length() > 0) lines.add(line.trim()); } return (String[]) lines.toArray(new String[0]); }