List of usage examples for java.lang StringBuffer deleteCharAt
@Override public synchronized StringBuffer deleteCharAt(int index)
From source file:com.easyjf.util.StringUtils.java
/** * Trim leading whitespace from the given String. * //from ww w.jav a 2s .c o m * @param str * the String to check * @return the trimmed String * @see java.lang.Character#isWhitespace */ public static String trimLeadingWhitespace(String str) { if (!hasLength(str)) return str; StringBuffer buf = new StringBuffer(str); while (buf.length() > 0 && Character.isWhitespace(buf.charAt(0))) buf.deleteCharAt(0); return buf.toString(); }
From source file:com.easyjf.util.StringUtils.java
/** * Trim leading and trailing whitespace from the given String. * /*www. j a va 2 s .c o m*/ * @param str * the String to check * @return the trimmed String * @see java.lang.Character#isWhitespace */ public static String trimWhitespace(String str) { if (!hasLength(str)) return str; StringBuffer buf = new StringBuffer(str); while (buf.length() > 0 && Character.isWhitespace(buf.charAt(0))) buf.deleteCharAt(0); while (buf.length() > 0 && Character.isWhitespace(buf.charAt(buf.length() - 1))) buf.deleteCharAt(buf.length() - 1); return buf.toString(); }
From source file:org.latticesoft.util.common.ConvertUtil.java
/** * Formats the string to fit specified length with the pad character * @param source the source string/*from w w w . j a v a 2 s. co m*/ * @param length the size of the padded string * @param padChar the character used for padding * @param isLeftPad if the padding is done at left side (true) or right side (false) * @param trimExcess if true the excess padded would be trimmed to fit exactly the length */ public static String formatString(String source, int length, String pad, boolean isLeftPad, boolean trimExcess) { StringBuffer sb = new StringBuffer(); if (source == null) { source = ""; } sb.append(source); int diff = length - sb.length(); if (diff <= 0) { return sb.toString(); } StringBuffer sb2 = new StringBuffer(); for (int i = 0; i < diff; i += pad.length()) { sb2.append(pad); } // trim excess padded characters // applicable if pad string is more than 1 character long diff = sb2.length() + sb.length() - length; if (diff > 0) { for (int i = 0; i < diff; i++) { sb2.deleteCharAt(sb2.length() - 1); } } // padd left or right if (isLeftPad) { sb2.append(sb.toString()); sb = sb2; } else { sb.append(sb2.toString()); } return sb.toString(); }
From source file:com.easyjf.util.StringUtils.java
/** * Trim trailing whitespace from the given String. * //from ww w. j a v a 2s .c o m * @param str * the String to check * @return the trimmed String * @see java.lang.Character#isWhitespace */ public static String trimTrailingWhitespace(String str) { if (!hasLength(str)) return str; StringBuffer buf = new StringBuffer(str); while (buf.length() > 0 && Character.isWhitespace(buf.charAt(buf.length() - 1))) buf.deleteCharAt(buf.length() - 1); return buf.toString(); }
From source file:org.exoplatform.wcm.webui.formgenerator.UIFormGeneratorTabPane.java
/** * Clean string./*w ww . java2 s .c o m*/ * * @param str the str * * @return the string */ private static String cleanString(String str) { Transliterator accentsconverter = Transliterator .getInstance("Latin; NFD; [:Nonspacing Mark:] Remove; NFC;"); str = accentsconverter.transliterate(str); //the character ? seems to not be changed to d by the transliterate function StringBuffer cleanedStr = new StringBuffer(str.trim()); // delete special character for (int i = 0; i < cleanedStr.length(); i++) { char c = cleanedStr.charAt(i); if (c == ' ') { if (i > 0 && cleanedStr.charAt(i - 1) == '-') { cleanedStr.deleteCharAt(i--); } else { c = '_'; cleanedStr.setCharAt(i, c); } continue; } if (i > 0 && !(Character.isLetterOrDigit(c) || c == '-')) { cleanedStr.deleteCharAt(i--); continue; } if (i > 0 && c == '-' && cleanedStr.charAt(i - 1) == '-') cleanedStr.deleteCharAt(i--); } if (!Character.isLetterOrDigit(cleanedStr.charAt(0))) { cleanedStr.deleteCharAt(0); } if (cleanedStr.length() > 0 && !Character.isLetterOrDigit(cleanedStr.charAt(cleanedStr.length() - 1))) { cleanedStr.deleteCharAt(cleanedStr.length() - 1); } return cleanedStr.toString().toLowerCase().replaceAll("-", "_"); }
From source file:org.snaker.modules.base.helper.SnakerJsonHelper.java
public static String getPathJson(List<TransitionModel> tms) { StringBuffer buffer = new StringBuffer(); buffer.append("paths:{"); for (TransitionModel tm : tms) { buffer.append(tm.getName());/*from ww w . ja va 2 s . c om*/ buffer.append(":{from:'"); buffer.append(tm.getSource().getName()); buffer.append("',to:'"); buffer.append(tm.getTarget().getName()); buffer.append("', dots:["); if (StringUtils.isNotEmpty(tm.getG())) { String[] bendpoints = tm.getG().split(";"); for (String bendpoint : bendpoints) { buffer.append("{"); String[] xy = bendpoint.split(","); buffer.append("x:").append(getNumber(xy[0])); buffer.append(",y:").append(xy[1]); buffer.append("},"); } buffer.deleteCharAt(buffer.length() - 1); } buffer.append("],text:{text:'"); buffer.append(tm.getDisplayName()); buffer.append("'},textPos:{"); if (StringUtils.isNotEmpty(tm.getOffset())) { String[] values = tm.getOffset().split(","); buffer.append("x:").append(values[0]).append(","); buffer.append("y:").append(values[1]).append(""); } buffer.append("}, props:{text:{value:''}}}"); buffer.append(","); } buffer.deleteCharAt(buffer.length() - 1); buffer.append("},"); return buffer.toString(); }
From source file:org.exoplatform.services.cms.impl.Utils.java
/** * Clean string./*from w ww. j a va 2 s .c o m*/ * * @param str the str * * @return the string */ public static String cleanString(String str) { Transliterator accentsconverter = Transliterator .getInstance("Latin; NFD; [:Nonspacing Mark:] Remove; NFC;"); str = accentsconverter.transliterate(str); //the character ? seems to not be changed to d by the transliterate function StringBuffer cleanedStr = new StringBuffer(str.trim()); // delete special character for (int i = 0; i < cleanedStr.length(); i++) { char c = cleanedStr.charAt(i); if (c == ' ') { if (i > 0 && cleanedStr.charAt(i - 1) == '-') { cleanedStr.deleteCharAt(i--); } else { c = '-'; cleanedStr.setCharAt(i, c); } continue; } if (i > 0 && !(Character.isLetterOrDigit(c) || c == '-')) { cleanedStr.deleteCharAt(i--); continue; } if (i > 0 && c == '-' && cleanedStr.charAt(i - 1) == '-') cleanedStr.deleteCharAt(i--); } while (StringUtils.isNotEmpty(cleanedStr.toString()) && !Character.isLetterOrDigit(cleanedStr.charAt(0))) { cleanedStr.deleteCharAt(0); } String clean = cleanedStr.toString().toLowerCase(); if (clean.endsWith("-")) { clean = clean.substring(0, clean.length() - 1); } return clean; }
From source file:net.lightbody.bmp.proxy.jetty.util.URI.java
/** Add two URI path segments. * Handles null and empty paths, path and query params (eg ?a=b or * ;JSESSIONID=xxx) and avoids duplicate '/' * @param p1 URI path segment /*from w w w. j av a 2s . co m*/ * @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 (p2 == null || p2.length() == 0) 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("/")) { buf.deleteCharAt(split - 1); buf.insert(split - 1, p2); } else buf.insert(split, p2); } else { if (p2.startsWith("/")) buf.insert(split, p2); else { buf.insert(split, '/'); buf.insert(split + 1, p2); } } return buf.toString(); }
From source file:com.liferay.util.Http.java
public static String parameterMapToStringNoEncode(Map parameterMap) { StringBuffer sb = new StringBuffer(); if (parameterMap.size() > 0) { sb.append(StringPool.QUESTION);/* www .j av a 2 s . c o m*/ Iterator itr = parameterMap.entrySet().iterator(); while (itr.hasNext()) { Map.Entry entry = (Map.Entry) itr.next(); String name = (String) entry.getKey(); String[] values = (String[]) entry.getValue(); for (int i = 0; i < values.length; i++) { sb.append(name); sb.append(StringPool.EQUAL); sb.append(values[i]); sb.append(StringPool.AMPERSAND); } } sb.deleteCharAt(sb.length() - 1); } return sb.toString(); }
From source file:org.eclim.plugin.jdt.util.JavaUtils.java
/** * Format a region in the supplied source file. * * @param src The ICompilationUnit./*from ww w. j a va2 s .c om*/ * @param kind The kind of code snippet to format. * @param offset The starting offset of the region to format. * @param length The length of the region to format. */ public static void format(ICompilationUnit src, int kind, int offset, int length) throws Exception { IBuffer buffer = src.getBuffer(); String contents = buffer.getContents(); String delimiter = StubUtility.getLineDelimiterUsed(src); DefaultCodeFormatter formatter = new DefaultCodeFormatter(src.getJavaProject().getOptions(true)); // when the eclipse indent settings differ from vim (tabs vs spaces) then // the inserted method's indent may be a bit off. this is a workaround to // force reformatting of the code from the start of the line to the start of // the next set of code following the new method. Doesn't quite fix indent // formatting of methods in nested classes. while (offset > 0 && !IndentManipulation.isLineDelimiterChar(buffer.getChar(offset - 1))) { offset--; length++; } while ((offset + length) < contents.length() && IndentManipulation.isLineDelimiterChar(buffer.getChar(offset + length))) { length++; } TextEdit edits = formatter.format(kind, contents, offset, length, 0, delimiter); if (edits != null) { int oldLength = contents.length(); Document document = new Document(contents); edits.apply(document); String formatted = document.get(); // jdt formatter can introduce trailing whitespace (javadoc comments), so // we'll remove all trailing whitespace from the formatted section. length += formatted.length() - oldLength; if (offset < (offset + length)) { String pre = formatted.substring(0, offset); StringBuffer section = new StringBuffer(formatted.substring(offset, offset + length)); StringBuffer post = new StringBuffer(formatted.substring(offset + length)); // account for section not ending at a line delimiter while (!section.toString().endsWith(delimiter) && post.length() > 0) { section.append(post.charAt(0)); post.deleteCharAt(0); } Matcher matcher = TRAILING_WHITESPACE.matcher(section); String stripped = matcher.replaceAll(StringUtils.EMPTY); src.getBuffer().setContents(pre + stripped + post); } else { src.getBuffer().setContents(formatted); } if (src.isWorkingCopy()) { src.commitWorkingCopy(true, null); } src.save(null, false); } }