List of usage examples for java.lang StringBuffer insert
@Override public StringBuffer insert(int offset, double d)
From source file:TextUtilities.java
/** * Pads the beginning of the given String with the given character until it's * <code>length</code> characters long. If the given String's size is already * <code>length</code> or larger, the given string is returned as is. */// ww w . ja v a2s . com public static String padStart(String s, char c, int length) { if (s.length() >= length) return s; StringBuffer buf = new StringBuffer(s); for (int i = s.length(); i < length; i++) buf.insert(0, c); return buf.toString(); }
From source file:TextUtilities.java
/** * Pads the given String on both sides equally (if possible) with the given * character until it's <code>length</code> characters long. If the given * String's size is already <code>length</code> or larger, the given * string is returned as is.//from www.j ava 2s . com */ public static String padSides(String s, char c, int length) { if (s.length() >= length) return s; StringBuffer buf = new StringBuffer(s); for (int i = s.length(); i < length - 1; i += 2) { buf.insert(0, c); buf.append(c); } if (buf.length() < length) buf.insert(0, c); return buf.toString(); }
From source file:Utils.java
/** * Add two URI path segments. Handles null and empty paths, path and query * params (eg ?a=b or ;JSESSIONID=xxx) and avoids duplicate '/' * // w w w. jav a 2 s . co m * @param p1 * URI path segment * @param p2 * URI path segment * @return Legally combined path segments. */ public static String addPaths(String p1, String p2) { if (p1 == null || p1.length() == 0) { if (p1 != null && p2 == null) return p1; return p2; } if (p2 == null || p2.length() == 0) return p1; int split = p1.indexOf(';'); if (split < 0) split = p1.indexOf('?'); if (split == 0) return p2 + p1; if (split < 0) split = p1.length(); StringBuffer buf = new StringBuffer(p1.length() + p2.length() + 2); buf.append(p1); if (buf.charAt(split - 1) == '/') { if (p2.startsWith(SLASH)) { buf.deleteCharAt(split - 1); buf.insert(split - 1, p2); } else buf.insert(split, p2); } else { if (p2.startsWith(SLASH)) buf.insert(split, p2); else { buf.insert(split, '/'); buf.insert(split + 1, p2); } } return buf.toString(); }
From source file:org.pmedv.core.util.StringUtil.java
/** * <p>/*w ww . j a v a2 s . c om*/ * Converts a straight text to a text with line breaks depending * on the length of a line. * </p> * <p> * If the character at <b>lineWidth</b> is not a whitespace, * the function steps back inside the character array until a * whitespace is found. * </p> * <p> * If the param html is set to true, the output string is embedded between * <html></html> tags and the line breaks are printed as <br> * instead of \n. This is for example useful for tooltip and html panel display * of the text. * <p> * * @param originalText the text to insert line breaks into * @param lineWidth the maximum length of the line (the expected position of the linebreak) * @param html force html output * * @return the converted String containing line breaks */ public static String insertLineBreaks(String originalText, int lineWidth, boolean html) { if (originalText == null) return ""; if (originalText.length() <= lineWidth) return originalText; StringBuffer text = new StringBuffer(originalText); if (html) { text.insert(0, Tags.HTML_OPEN); } for (int i = 1; i < text.length(); i++) { if (i % lineWidth == 0) { int j = i; while (text.charAt(j--) != ' ' && j > 1) ; // TODO : Remove hard coded line break and do some platform checking if (html) text.insert(j + 1, Tags.NEWLINE); else text.setCharAt(j + 1, '\n'); } } if (html) { text.append(Tags.HTML_CLOSE); } return text.toString(); }
From source file:Main.java
/** * Get the path to a given element. The path is a simplified version of XPath. It begins with the first element UNDER * the document element./* w w w. j a v a 2 s .com*/ * * @param elem Element of interest * @return XPath-like path to the element */ public static String getElementPath(Element elem) { if (elem == null) return null; StringBuffer sb = new StringBuffer(); Node parent = null; for (Node node = elem; node != null; node = parent) { parent = node.getParentNode(); if ((node instanceof Element) && (parent instanceof Element)) { if (sb.length() > 0) sb.insert(0, '/'); sb.insert(0, node.getNodeName()); } } return sb.toString(); }
From source file:BitLottoVerify.java
public static String printBytesInHex(byte[] d) { StringBuffer s = new StringBuffer(d.length * 2); BigInteger bi = new BigInteger(1, d); s.append(bi.toString(16));//from ww w. j a v a 2 s.com while (s.length() < d.length * 2) { s.insert(0, '0'); } return s.toString(); }
From source file:StringUtil.java
/** * <P>Right justify a string using the specified padding character to pad * the left part of the string until it reaches <code>maxLength</code>. * If the length of the string is greater than <code>maxLength</code> characters, * return only the first, left <code>maxLength</code> characters.</P> * * @return The right-justified string. *//*from w ww .j av a 2 s . com*/ public static String rightJustify(String s, int maxLength, char fill) { if (s == null || maxLength == 0) { return s; } // If the string has more than "maxLength" characters, // return only the first "maxLength" characters of the string. if (s.trim().length() > maxLength) { return s.substring(0, maxLength).trim(); } StringBuffer sb = new StringBuffer(s.trim()); // Insert as many padding characters as needed to reach "maxLength". while (sb.length() < maxLength) { sb.insert(0, fill); } return sb.toString(); }
From source file:Main.java
public static final String getComment(Element elem) { StringBuffer sb = new StringBuffer(); Node node = elem.getPreviousSibling(); while (node != null) { if (node.getNodeType() == Node.ELEMENT_NODE) { break; }/*from w w w .j a v a 2 s . c o m*/ if (node.getNodeType() == Node.COMMENT_NODE) { if (sb.length() > 0) { sb.insert(0, '\n'); sb.insert(0, ((Comment) node).getData()); } else { sb.append(((Comment) node).getData()); } } node = node.getPreviousSibling(); } return sb.toString(); }
From source file:org.apache.lucene.search.SearchUtil.java
public static String highlightSearchWord(String displayString, String searchQuery) { String[] searchQueryArray = null; String[] displayStringArray = null; searchQuery = StringUtils.replace(searchQuery, " ", " "); if (searchQuery.indexOf("\"") != -1 && searchQuery.lastIndexOf("\"") != -1) { searchQuery = searchQuery.replace("\"", ""); if (searchQuery.indexOf(" ") != -1) { String displayStringLowerCase = displayString.toLowerCase(); String searchQueryLowerCase = searchQuery.toLowerCase(); int searchQueryLocation = displayStringLowerCase.indexOf(searchQueryLowerCase); StringBuffer insertBlod = new StringBuffer(displayString); insertBlod.insert(searchQueryLocation + searchQuery.length(), "</strong>"); insertBlod.insert(searchQueryLocation, "<strong>"); displayString = insertBlod.toString(); return displayString; }//from www.j a va2 s. co m } searchQueryArray = searchQuery.split(" "); displayStringArray = displayString.split(" "); for (int i = 0; i < displayStringArray.length; i++) { for (int j = 0; j < searchQueryArray.length; j++) { if (displayStringArray[i].equalsIgnoreCase(searchQueryArray[j])) { displayStringArray[i] = "<strong>" + displayStringArray[i] + "</strong>"; } } } displayString = StringUtils.join(displayStringArray, " "); return displayString; }
From source file:br.gov.frameworkdemoiselle.util.contrib.Strings.java
public static String insertZeros(String string, int howMuchZeros) { StringBuffer result = new StringBuffer((string == null ? "" : string).trim()); int difference = howMuchZeros - string.length(); for (int j = 0; j < difference; j++) { result.insert(0, '0'); }//from w w w.ja va 2s .com return result.toString(); }