List of usage examples for java.lang StringBuilder lastIndexOf
@Override public int lastIndexOf(String str, int fromIndex)
From source file:Main.java
public static void main(String[] arg) { StringBuilder buffer = new StringBuilder("from java2s.com"); System.out.println(buffer.lastIndexOf("a", 10)); }
From source file:Main.java
public static String replace(String str, String target, String replacement) { StringBuilder sb = new StringBuilder(str); int index = sb.length(); int lenTarget = target.length(); while ((index = sb.lastIndexOf(target, index)) != -1) { sb.replace(index, index + lenTarget, replacement); index -= lenTarget;/*from w w w . ja v a 2 s .co m*/ } return sb.toString(); }
From source file:Main.java
public static String replace(String str, String[] targets, String[] replacements) { StringBuilder sb = new StringBuilder(str); int index, lenTarget; for (int i = 0; i < targets.length; i++) { index = sb.length();/*from w ww .j a v a 2s. c o m*/ lenTarget = targets[i].length(); while ((index = sb.lastIndexOf(targets[i], index)) != -1) { sb.replace(index, index + lenTarget, replacements[i]); index -= lenTarget; } } return sb.toString(); }
From source file:com.jaspersoft.jasperserver.core.util.StringUtil.java
/** * Replaces all occurrences of a string in a buffer with another. * * @param buf String buffer to act on/*from www . j av a2 s.c o m*/ * @param start Ordinal within <code>find</code> to start searching * @param find String to find * @param replace String to replace it with * @return The string buffer */ public static StringBuilder replace(StringBuilder buf, int start, String find, String replace) { // Search and replace from the end towards the start, to avoid O(n ^ 2) // copying if the string occurs very commonly. int findLength = find.length(); if (findLength == 0) { // Special case where the seek string is empty. for (int j = buf.length(); j >= 0; --j) { buf.insert(j, replace); } return buf; } int k = buf.length(); while (k > 0) { int i = buf.lastIndexOf(find, k); if (i < start) { break; } buf.replace(i, i + find.length(), replace); // Step back far enough to ensure that the beginning of the section // we just replaced does not cause a match. k = i - findLength; } return buf; }
From source file:Main.java
private static String removeDotSegments(StringBuilder uri, int offset, int limit) { if (offset >= limit) { return uri.toString(); }/* w ww .j a v a 2 s .co m*/ if (uri.charAt(offset) == '/') { offset++; } int segmentStart = offset; int i = offset; while (i <= limit) { int nextSegmentStart; if (i == limit) { nextSegmentStart = i; } else if (uri.charAt(i) == '/') { nextSegmentStart = i + 1; } else { i++; continue; } if (i == segmentStart + 1 && uri.charAt(segmentStart) == '.') { uri.delete(segmentStart, nextSegmentStart); limit -= nextSegmentStart - segmentStart; i = segmentStart; } else if (i == segmentStart + 2 && uri.charAt(segmentStart) == '.' && uri.charAt(segmentStart + 1) == '.') { int prevSegmentStart = uri.lastIndexOf("/", segmentStart - 2) + 2; int removeFrom = prevSegmentStart > offset ? prevSegmentStart : offset; uri.delete(removeFrom, nextSegmentStart); limit -= nextSegmentStart - removeFrom; i = prevSegmentStart; } else { i++; segmentStart = i; } } return uri.toString(); }
From source file:com.springrts.springls.Client.java
/** * Removes carriage-return chars (first part of windows EOL "\r\n"). * @param str where to search in/*w ww. j av a 2 s.co m*/ * @param posUntil only chars from 0 until this position are searched * @return number of deleted chars */ private static int deleteCarriageReturnChars(StringBuilder str, int posUntil) { int deleted = 0; int rPos = str.lastIndexOf("\r", posUntil); while (rPos != -1) { str.deleteCharAt(rPos); deleted++; rPos = str.lastIndexOf("\r", rPos); } return deleted; }
From source file:edu.uci.ics.jung.visualization.util.LabelWrapper.java
/** * line-wrap the passed String as an html string with * break Strings inserted./* w w w . j av a2 s . c om*/ * * @param str * @return */ private String wrap(String str) { StringBuilder buf = new StringBuilder(str); int len = lineLength; while (len < buf.length()) { int idx = buf.lastIndexOf(" ", len); if (idx != -1) { buf.replace(idx, idx + 1, breaker); len = idx + breaker.length() + lineLength; } else { buf.insert(len, breaker); len += breaker.length() + lineLength; } } buf.insert(0, "<html>"); return buf.toString(); }
From source file:org.shredzone.cilla.web.tag.FormatTag.java
@Override public int doEndTag() throws JspException { CharSequence result;//from w w w. jav a 2 s . c o m TextFormat txtFormat; if (text != null && text instanceof FormattedText) { if (format != null) { throw new IllegalArgumentException("text contains FormattedText, format must not be set"); } result = ((FormattedText) text).getText(); txtFormat = ((FormattedText) text).getFormat(); } else { if (format != null && format instanceof TextFormat) { txtFormat = (TextFormat) format; } else if (format != null) { txtFormat = TextFormat.valueOf(format.toString()); } else if (text == null) { // if no text and no format was set, render the body as HTML txtFormat = TextFormat.HTML; } else { throw new IllegalArgumentException("format not set"); } if (text != null) { result = text.toString(); } else if (bodyContent != null) { result = bodyContent.toString().trim(); } else { result = ""; } } if (result == null) result = ""; result = textFormatter.format(result, txtFormat, () -> linkService.linkTo().page(page)); if (stripHtml) { result = textFormatter.stripHtml(result); } if (truncate != null && result.length() > truncate) { StringBuilder trunc = new StringBuilder(result); int truncpos = trunc.lastIndexOf(" ", truncate); if (truncpos < truncate - 30) { truncpos = truncate; } trunc.setLength(truncpos); trunc.append("\u2026"); result = trunc; } if (var != null) { TagUtils.setScopedAttribute(pageContext, var, result, scope); } else { try { pageContext.getOut().print(result); } catch (IOException ex) { throw new JspException(ex); } } return EVAL_PAGE; }
From source file:com.plugin.excel.types.ExcelCell.java
public String getDocumentation() { if (StringUtils.isNotBlank(documentation)) { StringBuilder sb = new StringBuilder(documentation); int i = 0; int length = 70; while (i + length < sb.length() && (i = sb.lastIndexOf(" ", i + length)) != -1) { sb.replace(i, i + 1, "\n"); }/*w w w . ja va 2 s .c o m*/ return sb.toString(); } return documentation; }
From source file:Main.java
/** * Removes dot segments from the path of a URI. * * @param uri A {@link StringBuilder} containing the URI. * @param offset The index of the start of the path in {@code uri}. * @param limit The limit (exclusive) of the path in {@code uri}. *///w w w . ja v a 2 s .co m private static String removeDotSegments(StringBuilder uri, int offset, int limit) { if (offset >= limit) { // Nothing to do. return uri.toString(); } if (uri.charAt(offset) == '/') { // If the path starts with a /, always retain it. offset++; } // The first character of the current path segment. int segmentStart = offset; int i = offset; while (i <= limit) { int nextSegmentStart = -1; if (i == limit) { nextSegmentStart = i; } else if (uri.charAt(i) == '/') { nextSegmentStart = i + 1; } else { i++; continue; } // We've encountered the end of a segment or the end of the path. If the final segment was // "." or "..", remove the appropriate segments of the path. if (i == segmentStart + 1 && uri.charAt(segmentStart) == '.') { // Given "abc/def/./ghi", remove "./" to get "abc/def/ghi". uri.delete(segmentStart, nextSegmentStart); limit -= nextSegmentStart - segmentStart; i = segmentStart; } else if (i == segmentStart + 2 && uri.charAt(segmentStart) == '.' && uri.charAt(segmentStart + 1) == '.') { // Given "abc/def/../ghi", remove "def/../" to get "abc/ghi". int prevSegmentStart = uri.lastIndexOf("/", segmentStart - 2) + 1; int removeFrom = prevSegmentStart > offset ? prevSegmentStart : offset; uri.delete(removeFrom, nextSegmentStart); limit -= nextSegmentStart - removeFrom; segmentStart = prevSegmentStart; i = prevSegmentStart; } else { i++; segmentStart = i; } } return uri.toString(); }