Example usage for java.lang StringBuffer substring

List of usage examples for java.lang StringBuffer substring

Introduction

In this page you can find the example usage for java.lang StringBuffer substring.

Prototype

@Override
public synchronized String substring(int start, int end) 

Source Link

Usage

From source file:raptor.util.RaptorStringUtils.java

/**
 * Returns a comma delimited array of strings.
 *//*from   w  ww.j  a v a 2s  .  c  o  m*/
public static String toString(String[] values) {
    String valuesString = null;
    StringBuffer valuesBuffer = new StringBuffer();
    for (String value : values) {
        valuesBuffer.append(value).append(",");
    }
    if (StringUtils.isNotBlank(valuesBuffer.toString())) {
        valuesString = valuesBuffer.substring(0, valuesBuffer.length() - 1);
    }
    return valuesString == null ? valuesBuffer.toString() : valuesString;
}

From source file:Main.java

private static boolean isEncodeable(StringBuffer p_src, int index) {
    // white space
    boolean blank = false;
    boolean lt = false;
    String sub = null;//  w  w  w.  j  a va2  s.  co m

    for (int i = index - 1; i >= 0; i--) {
        char c = p_src.charAt(i);
        String cstr = "" + c;

        if ("".equals(cstr.trim())) {
            blank = true;
        } else if (c == '<') {
            sub = p_src.substring(i, index);
            lt = true;
            break;
        }
        // node value, encode
        else if (c == '>') {
            return true;
        }
    }

    // node name, do not encode
    if (lt && !blank) {
        return false;
    }

    // comments, encode
    if (sub.startsWith("<!--")) {
        return true;
    }

    // cdata, encode
    if (sub.startsWith("<![CDATA[")) {
        return true;
    }

    // dtd or version, do not encode
    if (sub.startsWith("<!") || sub.startsWith("<?")) {
        return false;
    }

    // determine attribute
    int subLen = sub.length();
    boolean eq = false;
    boolean singleQuote = false;
    boolean doubleQuote = false;
    for (int i = 1; i < subLen; i++) {
        char c = sub.charAt(i);

        if (c == '=' && !singleQuote && !doubleQuote) {
            eq = true;
        }

        if (c == '\'' && eq && !doubleQuote) {
            singleQuote = !singleQuote;
            if (!singleQuote) {
                eq = false;
            }
        }

        if (c == '"' && eq && !singleQuote) {
            doubleQuote = !doubleQuote;
            if (!doubleQuote) {
                eq = false;
            }
        }
    }

    if (singleQuote || doubleQuote) {
        return true;
    } else {
        return false;
    }
}

From source file:com.boyuanitsm.pay.unionpay.util.SDKUtil.java

/**
 * Map???Keyascii???key1=value1&key2=value2? ????signature
 * //from  www .j  av  a  2 s  .co  m
 * @param data
 *            Map?
 * @return ?
 */
public static String coverMap2String(Map<String, String> data) {
    TreeMap<String, String> tree = new TreeMap<String, String>();
    Iterator<Entry<String, String>> it = data.entrySet().iterator();
    while (it.hasNext()) {
        Entry<String, String> en = it.next();
        if (SDKConstants.param_signature.equals(en.getKey().trim())) {
            continue;
        }
        tree.put(en.getKey(), en.getValue());
    }
    it = tree.entrySet().iterator();
    StringBuffer sf = new StringBuffer();
    while (it.hasNext()) {
        Entry<String, String> en = it.next();
        sf.append(en.getKey() + SDKConstants.EQUAL + en.getValue() + SDKConstants.AMPERSAND);
    }
    if (sf.length() == 0) {
        return "";
    }
    return sf.substring(0, sf.length() - 1);
}

From source file:Main.java

/**
 * Convert entities to umlaute//from  w w w. j  a va 2  s. co  m
 */
public static String decode(String value) {
    StringBuffer buffer = new StringBuffer(value);
    int pos;
    boolean found;
    for (int i = 0; i < buffer.length(); i++) {
        if (buffer.charAt(i) == '_' && buffer.charAt(i + 1) == '_') {
            pos = i + 2;
            found = false;
            while (buffer.charAt(pos) >= '0' && buffer.charAt(pos) <= '9') {
                found = true;
                pos++;
            }
            if (found == true && pos > i + 2 && buffer.charAt(pos) == ';') {
                int ent = new Integer(buffer.substring(i + 2, pos)).intValue();
                buffer.replace(i, pos + 1, "" + (char) ent);
            }
        }
    }
    return buffer.toString();
}

From source file:de.tudarmstadt.ukp.dkpro.tc.core.util.ReportUtils.java

/**
 * Converts a bipartition array into a list of class names. Parameter arrays must have the same
 * length/*from   w  w  w.  j  a  v a 2s .c om*/
 * 
 * @param labels
 * @param classNames
 * @return
 */
public static String doubleArrayToClassNames(int[] labels, String[] classNames, Character separatorChar) {
    StringBuffer buffer = new StringBuffer();

    for (int y = 0; y < labels.length; y++) {
        if (labels[y] == 1) {
            buffer.append(classNames[y] + separatorChar);
        }
    }
    String classString;
    try {
        classString = buffer.substring(0, buffer.length() - 1).toString();
    } catch (StringIndexOutOfBoundsException e) {
        classString = "";
    }
    return classString;
}

From source file:org.soitoolkit.commons.mule.util.MiscUtil.java

static public String parseStringValue(String strVal, Properties props) {

    StringBuffer buf = new StringBuffer(strVal);

    int startIndex = strVal.indexOf(placeholderPrefix);
    while (startIndex != -1) {
        int endIndex = findPlaceholderEndIndex(buf, startIndex);
        if (endIndex != -1) {
            String placeholder = buf.substring(startIndex + placeholderPrefix.length(), endIndex);

            String propVal = props.getProperty(placeholder);

            if (propVal != null) {

                // Recursive invocation, parsing placeholders contained in the previously resolved placeholder value.
                // E.g. a variable value like: VARIABLE1=Var${VARIABLE2}Value
                propVal = parseStringValue(propVal, props);

                buf.replace(startIndex, endIndex + placeholderSuffix.length(), propVal);
                if (logger.isTraceEnabled()) {
                    logger.trace("Resolved placeholder '" + placeholder + "'");
                }//from  w w  w. j a  v a2  s.  c  o m
                startIndex = buf.indexOf(placeholderPrefix, startIndex + propVal.length());
            } else {
                throw new RuntimeException("Could not resolve placeholder '" + placeholder + "'");
            }
        } else {
            startIndex = -1;
        }
    }
    return buf.toString();
}

From source file:org.apache.hadoop.tools.util.DistCpUtils.java

/**
 * Pack file preservation attributes into a string, containing
 * just the first character of each preservation attribute
 * @param attributes - Attribute set to preserve
 * @return - String containing first letters of each attribute to preserve
 *///from   w w w.jav  a2 s .c  o m
public static String packAttributes(EnumSet<FileAttribute> attributes) {
    StringBuffer buffer = new StringBuffer(FileAttribute.values().length);
    int len = 0;
    for (FileAttribute attribute : attributes) {
        buffer.append(attribute.name().charAt(0));
        len++;
    }
    return buffer.substring(0, len);
}

From source file:com.flattr4android.rest.FlattrRestClient.java

private static String getParameterString(String paramName, List<String> params) {
    if ((params == null) || (params.size() <= 0)) {
        return "";
    }/*  ww w . j a v a 2s  .  c o m*/
    StringBuffer result = new StringBuffer("/");
    result.append(paramName);
    result.append("/");
    for (String s : params) {
        result.append(s);
        result.append(",");
    }
    return result.substring(0, result.length() - 1);
}

From source file:org.springframework.util.SystemPropertyUtils.java

/**
 * Resolve ${...} placeholders in the given text,
 * replacing them with corresponding system property values.
 * @param text the String to resolve/*from   w  w  w  .  j  a v a  2  s.  c  o m*/
 * @return the resolved String
 * @see #PLACEHOLDER_PREFIX
 * @see #PLACEHOLDER_SUFFIX
 */
public static String resolvePlaceholders(String text) {
    StringBuffer buf = new StringBuffer(text);

    // The following code does not use JDK 1.4's StringBuffer.indexOf(String)
    // method to retain JDK 1.3 compatibility. The slight loss in performance
    // is not really relevant, as this code will typically just run on startup.

    int startIndex = text.indexOf(PLACEHOLDER_PREFIX);
    while (startIndex != -1) {
        int endIndex = buf.toString().indexOf(PLACEHOLDER_SUFFIX, startIndex + PLACEHOLDER_PREFIX.length());
        if (endIndex != -1) {
            String placeholder = buf.substring(startIndex + PLACEHOLDER_PREFIX.length(), endIndex);
            String propVal = System.getProperty(placeholder);
            if (propVal != null) {
                buf.replace(startIndex, endIndex + PLACEHOLDER_SUFFIX.length(), propVal);
                startIndex = buf.toString().indexOf(PLACEHOLDER_PREFIX, startIndex + propVal.length());
            } else {
                if (logger.isWarnEnabled()) {
                    logger.warn("Could not resolve placeholder '" + placeholder + "' in [" + text
                            + "] as system property");
                }
                startIndex = buf.toString().indexOf(PLACEHOLDER_PREFIX, endIndex + PLACEHOLDER_SUFFIX.length());
            }
        } else {
            startIndex = -1;
        }
    }

    return buf.toString();
}

From source file:br.com.diegosilva.jsfcomponents.util.Utils.java

public static String truncateStringOnWordBoundary(String string, int length) {
    if (string.length() <= length)
        return string;

    char[] chars = string.toCharArray();
    StringBuffer buffer = new StringBuffer();
    String result = "";
    int lastWhitespace = 0;
    for (int i = 0; i < chars.length; i++) {
        if (chars[i] == ' ')
            lastWhitespace = i;//from w  w w . j  a v a2s  .c o  m
        buffer.append(chars[i]);

        if (i >= length) {
            result = buffer.substring(0, lastWhitespace);
            break;
        }
    }
    return result;
}