Example usage for java.lang StringBuffer replace

List of usage examples for java.lang StringBuffer replace

Introduction

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

Prototype

@Override
public synchronized StringBuffer replace(int start, int end, String str) 

Source Link

Usage

From source file:StringUtils.java

/**
 *  Replaces a part of a string with a new String.
 *
 *  @param start Where in the original string the replacing should start.
 *  @param end Where the replacing should end.
 *  @param orig Original string.  Null is safe.
 *  @param text The new text to insert into the string.
 *  @return The string with the orig replaced with text.
 *//*from   w  w w.  ja  v  a  2 s .co m*/
public static String replaceString(String orig, int start, int end, String text) {
    if (orig == null)
        return null;

    StringBuffer buf = new StringBuffer(orig);

    buf.replace(start, end, text);

    return buf.toString();
}

From source file:Main.java

/**
 * Convert umlaute to entities//from   w ww  .  j av a 2  s  .  c o m
 */
public static String encode(String value) {
    StringBuffer buffer = new StringBuffer(value);
    for (int i = 0; i < buffer.length(); i++) {
        if (buffer.charAt(i) > 127) {
            buffer.replace(i, i + 1, "__" + ((int) buffer.charAt(i)) + ";");
        }
    }
    return buffer.toString();
}

From source file:Main.java

/**
 * XML encode a text string.//from w ww  .j ava2 s .  c  om
 * 
 * @param text - a String.
 * @return an XML encoded text string.
 */
public static String xmlRemoveEscapedString(String text) {
    if (text == null)
        return text;
    else {
        StringBuffer sb = new StringBuffer(text);

        int i = sb.indexOf("&#xd;");
        while (i != -1) {
            sb.replace(i, i + 5, "\r");
            i = sb.indexOf("&#xd;");
        }

        i = sb.indexOf("&lt;");
        while (i != -1) {
            sb.replace(i, i + 4, "<");
            i = sb.indexOf("&lt;");
        }

        i = sb.indexOf("&gt;");
        while (i != -1) {
            sb.replace(i, i + 4, ">");
            i = sb.indexOf("&gt;");
        }

        i = sb.indexOf("&quot;");
        while (i != -1) {
            sb.replace(i, i + 6, "\"");
            i = sb.indexOf("&quot;");
        }

        i = sb.indexOf("&apos;");
        while (i != -1) {
            sb.replace(i, i + 6, "\'");
            i = sb.indexOf("&apos;");
        }

        i = sb.indexOf("&amp;");
        while (i != -1) {
            sb.replace(i, i + 5, "&");
            i = sb.indexOf("&amp;");
        }
        return sb.toString();
    }
}

From source file:Main.java

public static String getXmlEncoded(String text) {
    StringBuffer buf = new StringBuffer(text);
    for (int i = 0; i < buf.length(); ++i) {
        char c = buf.charAt(i);
        if (c == '&') {
            buf.replace(i, i + 1, "&amp;");
        } else if (c == '<') {
            buf.replace(i, i + 1, "&lt;");
        } else if (c == '>') {
            buf.replace(i, i + 1, "&gt;");
        } else if (c == '"') {
            buf.replace(i, i + 1, "&quot;");
        } else if (c == '\'') {
            buf.replace(i, i + 1, "&#39;");
        }//from www .  j a v a  2s. com
    }
    return buf.toString();
}

From source file:org.kuali.rice.devtools.generators.dd.MaintDocDDCreator.java

public static String camelCaseToHelpParm(String className) {
    StringBuffer newName = new StringBuffer(className);
    // lower case the 1st letter
    newName.replace(0, 1, newName.substring(0, 1).toLowerCase());
    // loop through, inserting spaces when cap
    for (int i = 0; i < newName.length(); i++) {
        if (Character.isUpperCase(newName.charAt(i))) {
            newName.insert(i, '_');
            i++;/*www . j ava  2 s  .c om*/
        }
    }
    return newName.toString().toUpperCase().trim();
}

From source file:StringUtils.java

/**
 * Replaces each substring of this string that matches toReplace.
 * Used to replace replaceAll when using J2SDK 1.3.1.
 * This method is available at String.replaceAll in J2SDK 1.4
 * @param theString The string to use//from   w  w w. j a  v a2 s .  c o  m
 * @param toReplace The string to replace.
 * @param replacement The replacement string.
 * @return The updated string after replacing.
 */
public static String replaceAll(String theString, String toReplace, String replacement) {
    if (theString == null) {
        return null;
    }
    if (theString.indexOf(toReplace) == -1) {
        return theString;
    }

    StringBuffer stringBuffer = new StringBuffer(theString);
    int index = theString.length();
    int offset = toReplace.length();
    while ((index = theString.lastIndexOf(toReplace, index - 1)) > -1) {
        stringBuffer.replace(index, index + offset, replacement);
    }

    return stringBuffer.toString();
}

From source file:Main.java

/** Looks in findin for all occurrences of find and replaces them with replacewith 
 * @param findin The string to find occurrences in
 * @param find The string to find// w w w .  j a  v a 2  s. c  o m
 * @param replacewith The string to replace found occurrences with
 * @return A string with all occurrences of find replaced.
 */
public static String replace(String findin, String find, String replacewith) {

    StringBuffer sb = new StringBuffer(findin);
    int i = 0;
    try {
        while (i <= sb.length() - find.length()) {
            if (sb.substring(i, i + find.length()).equalsIgnoreCase(find)) {
                sb.replace(i, i + find.length(), replacewith);
            }
            i++;
        }
    } catch (StringIndexOutOfBoundsException e) {
        // We hit the end of the string - do nothing and carry on
    }

    return sb.toString();
}

From source file:org.lexevs.dao.indexer.utility.Utility.java

public static String replaceStringsInString(StringBuffer input, String searchFor, String replaceWith) {
    // int loc = input.indexOf(searchFor);//jdk 1.3 compliance... grrr
    int loc = input.toString().indexOf(searchFor);
    while (loc != -1) {
        input.replace(loc, loc + searchFor.length(), replaceWith);
        // loc = input.indexOf(searchFor);
        loc = input.toString().indexOf(searchFor);
    }/*from  w w w .java2 s .c o m*/
    return input.toString();
}

From source file:org.kuali.ext.mm.utility.BeanDDCreator.java

public static String camelCaseToString(String className) {
    StringBuffer newName = new StringBuffer(className);
    // upper case the 1st letter
    newName.replace(0, 1, newName.substring(0, 1).toUpperCase());
    // loop through, inserting spaces when cap
    for (int i = 0; i < newName.length(); i++) {
        if (Character.isUpperCase(newName.charAt(i))) {
            newName.insert(i, ' ');
            i++;//w  ww .j  a  va 2s . co m
        }
    }

    return newName.toString().trim().replace("Uc", "UC");
}

From source file:cn.ctyun.amazonaws.util.StringUtils.java

public static String replace(String originalString, String partToMatch, String replacement) {
    StringBuffer buffer = new StringBuffer(originalString.length());
    buffer.append(originalString);//  ww  w. ja va2 s  . com

    int indexOf = buffer.indexOf(partToMatch);
    while (indexOf != -1) {
        buffer = buffer.replace(indexOf, indexOf + partToMatch.length(), replacement);
        indexOf = buffer.indexOf(partToMatch);
    }

    return buffer.toString();
}