Example usage for java.lang StringBuffer insert

List of usage examples for java.lang StringBuffer insert

Introduction

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

Prototype

@Override
public StringBuffer insert(int offset, double d) 

Source Link

Usage

From source file:org.omnaest.utils.strings.StringUtils.java

public static String insertString(String baseString, String insertString, int insertPosition,
        boolean overwrite) {
    ///*from   w w w .  jav a  2s.c o m*/
    String retval = null;

    //
    if (insertPosition < baseString.length()) {
        StringBuffer sb = new StringBuffer(baseString);
        for (int iInsertPosition = insertPosition; iInsertPosition < insertPosition + insertString.length()
                && iInsertPosition < baseString.length(); iInsertPosition++) {
            int insertStringSourcePosition = iInsertPosition - insertPosition;
            if (overwrite) {
                sb.deleteCharAt(iInsertPosition);
            }
            sb.insert(iInsertPosition,
                    insertString.substring(insertStringSourcePosition, insertStringSourcePosition + 1));
        }
        retval = sb.toString();
    } else {
        throw new StringIndexOutOfBoundsException();
    }

    //
    return retval;
}

From source file:jdiff.API.java

/**
 * Convert all HTML tags to text by stuffing text into the HTML tag
 * to stop it being an HTML or XML tag. E.g. &quot;<code>foo</code>&quot;
 * becomes &quot;lEsS_tHaNcode>foolEsS_tHaN/code>&quot;. Replace all &lt; 
 * characters// w  ww. j  av  a2 s.c o m
 * with the string "lEsS_tHaN". Also replace &amp; character with the  
 * string "aNd_cHaR" to avoid text entities. Also replace &quot; 
 * character with the  
 * string "qUoTe_cHaR".
 */
public static String hideHTMLTags(String htmlText) {
    StringBuffer sb = new StringBuffer(htmlText);
    int i = 0;
    while (i < sb.length()) {
        if (sb.charAt(i) == '<') {
            sb.setCharAt(i, 'l');
            sb.insert(i + 1, "EsS_tHaN");
        } else if (sb.charAt(i) == '&') {
            sb.setCharAt(i, 'a');
            sb.insert(i + 1, "Nd_cHaR");
        } else if (sb.charAt(i) == '"') {
            sb.setCharAt(i, 'q');
            sb.insert(i + 1, "uote_cHaR");
        }
        i++;
    }
    return sb.toString();
}

From source file:Main.java

/**
 * Generate a human friendly time string based on milliseconds
 *//* w  ww  . j a va  2s .  c o m*/
public static String timeString(long duration) {

    long ONE_SECOND = 1000;
    long ONE_MINUTE = ONE_SECOND * 60;
    long ONE_HOUR = ONE_MINUTE * 60;
    long ONE_DAY = ONE_HOUR * 24;

    StringBuffer res = new StringBuffer();
    long temp = 0;

    if (duration >= ONE_DAY * 99) {
        return "Never updated";
    }

    if (duration >= ONE_MINUTE) {
        temp = duration / ONE_DAY;
        if (temp > 0) {
            duration -= temp * ONE_DAY;
            res.append(temp).append(" day");
        } else {
            temp = duration / ONE_HOUR;
            if (temp > 0) {
                duration -= temp * ONE_HOUR;
                res.append(temp).append(" hr");
            } else {
                temp = duration / ONE_MINUTE;
                if (temp > 0) {
                    duration -= temp * ONE_MINUTE;
                    res.append(temp).append(" min");
                }
            }
        }

        res.insert(0, "Updated ");
        res.append(" ago");
        return res.toString();
    } else {
        return "Just updated";
    }
}

From source file:com.sfs.whichdoctor.formatter.OutputFormatter.java

/**
 * To formatted vote number./*from   w  w w .  j ava2 s  .c o  m*/
 *
 * @param voteNumber the vote number
 *
 * @return the string
 */
public static String toFormattedVoteNumber(final int voteNumber) {
    final StringBuffer vote = new StringBuffer();

    vote.append(voteNumber);

    final int maxVoteLength = 6;

    while (vote.length() < maxVoteLength) {
        // Pad the number with zeros to make it at least 6 characters
        vote.insert(0, "0");
    }
    return vote.toString();
}

From source file:org.ccnx.ccn.impl.support.DataUtils.java

/**
 * @param inputString/*from  w  ww .jav a 2s  .  co  m*/
 * @param lineLength
 * @return
 */
public static String lineWrap(String inputString, int lineLength) {
    if ((null == inputString) || (inputString.length() <= lineLength)) {
        return inputString;
    }

    StringBuffer line = new StringBuffer(inputString);

    int length = inputString.length();
    int sepLen = LINE_SEPARATOR.length();
    int index = lineLength - sepLen;
    while (index < length - sepLen) {
        line.insert(index, LINE_SEPARATOR);
        index += lineLength;
        length += sepLen;
    }
    return line.toString();
}

From source file:org.jboss.drools.guvnor.importgenerator.PackageFile.java

/**
 * returns "approval.determine" where path is /home/mallen/workspace/rules/approval/determine/1.0.0.SNAPSHOT/file.xls
 * and options "start" is "rules" and end is "[0-9|.]*[SNAPSHOT]+[0-9|.]*" ie. any number, dot and word SNAPSHOT
 *
 * @param directory//www  . j  a v  a 2s .c  o m
 * @param options
 * @return
 */
private static String getPackageName(File directory, CmdArgsParser options) {
    String startPath = directory.getPath();
    Matcher m = Pattern.compile("([^/|\\\\]+)").matcher(startPath); // quad-backslash is for windows paths
    List<String> lpath = new ArrayList<String>();
    while (m.find())
        lpath.add(m.group());
    String[] path = lpath.toArray(new String[lpath.size()]);
    StringBuffer sb = new StringBuffer();
    for (int i = path.length - 1; i >= 0; i--) {
        String dir = path[i];
        if ((dir.matches(options.getOption(Parameters.OPTIONS_PACKAGE_EXCLUDE))))
            continue;
        if ((dir.equals(options.getOption(Parameters.OPTIONS_PACKAGE_START))))
            break; //since we are working in reverse, it's time to exit
        sb.insert(0, PACKAGE_DELIMETER).insert(0, dir);
    }
    if (sb.substring(sb.length() - 1).equals(PACKAGE_DELIMETER))
        sb.delete(sb.length() - 1, sb.length());

    return sb.toString();
}

From source file:org.apache.myfaces.application.jsp.JspStateManagerImpl.java

private static String getPathToComponent(UIComponent component) {
    StringBuffer buf = new StringBuffer();

    if (component == null) {
        buf.append("{Component-Path : ");
        buf.append("[null]}");
        return buf.toString();
    }//  www . j  a va 2 s .co m

    getPathToComponent(component, buf);

    buf.insert(0, "{Component-Path : ");
    buf.append("}");

    return buf.toString();
}

From source file:edu.harvard.i2b2.analysis.security.HighEncryption.java

public static String leftPad(String stringToPad, int size, String padder) {
    if (padder.length() == 0) {
        return stringToPad;
    }//from w w w  .  j  a va2  s  .c  o  m
    StringBuffer strb = new StringBuffer(size);
    StringCharacterIterator sci = new StringCharacterIterator(padder);

    while (strb.length() < (size - stringToPad.length())) {
        for (char ch = sci.first(); ch != CharacterIterator.DONE; ch = sci.next()) {
            if (strb.length() < size - stringToPad.length()) {
                strb.insert(strb.length(), String.valueOf(ch));
            }
        }
    }
    return strb.append(stringToPad).toString();
}

From source file:net.unicon.demetrius.fac.filesystem.FsResourceFactory.java

private static String evaluateFsPath(String dataSource, IResource r) {

    // Assertions.
    if (dataSource == null) {
        String msg = "Argument 'dataSource' cannot be null.";
        throw new IllegalArgumentException(msg);
    }// w w  w .j  ava2s  .  com
    if (r == null) {
        String msg = "Argument 'r [IResource]' cannot be null.";
        throw new IllegalArgumentException(msg);
    }

    StringBuffer rslt = new StringBuffer();
    IResource loop = r;
    while (loop.getParent() != null) {
        rslt.insert(0, loop.getName());
        rslt.insert(0, DELIMITER);
        loop = loop.getParent();
    }
    rslt.insert(0, dataSource);

    return rslt.toString();

}

From source file:com.t3.model.MacroButtonProperties.java

public static String paddingString(String s, int n, char c, boolean paddingLeft) {
    StringBuffer str = new StringBuffer(s);
    int strLength = str.length();
    if (n > 0 && n > strLength) {
        for (int i = 0; i <= n; i++) {
            if (paddingLeft) {
                if (i < n - strLength)
                    str.insert(0, c);
            } else {
                if (i > strLength)
                    str.append(c);//from  ww w  . java 2s .  c  om
            }
        }
    }
    return str.toString();
}