Example usage for java.lang StringBuilder insert

List of usage examples for java.lang StringBuilder insert

Introduction

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

Prototype

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

Source Link

Usage

From source file:Main.java

public static String formatFromSize(long size) {
    if (size == -1)
        return "";
    String suffix = " B";
    if (size >= 1024) {
        suffix = " KB";
        size /= 1024;//ww w  .  j a  v  a  2s  .  c o  m
        if (size >= 1024) {
            suffix = " MB";
            size /= 1024;
        }
    }

    StringBuilder resultBuffer = new StringBuilder(Long.toString(size));

    int commaOffset = resultBuffer.length() - 3;
    while (commaOffset > 0) {
        resultBuffer.insert(commaOffset, ',');
        commaOffset -= 3;
    }

    if (suffix != null)
        resultBuffer.append(suffix);
    return resultBuffer.toString();
}

From source file:com.hortonworks.pso.data.generator.fields.IPAddressField.java

public static String longToIp(long ip) {
    StringBuilder sb = new StringBuilder(15);

    for (int i = 0; i < 4; i++) {
        sb.insert(0, Long.toString(ip & 0xff));

        if (i < 3) {
            sb.insert(0, '.');
        }//w w w. jav  a  2s .  c  o m

        ip >>= 8;
    }

    return sb.toString();
}

From source file:Main.java

public static String toUUIDFormat(byte[] bytes) {
    byte[] switched = new byte[] { bytes[3], bytes[2], bytes[1], bytes[0], bytes[5], bytes[4], bytes[7],
            bytes[6], bytes[8], bytes[9], bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15] };

    StringBuilder sb = new StringBuilder(bytes.length * 2);
    Formatter formatter = new Formatter(sb);
    for (byte b : switched) {
        formatter.format("%02x", b);
    }/*  w  w  w.  j a v  a2  s  .  c o  m*/
    sb.insert(8, "-");
    sb.insert(13, "-");
    sb.insert(18, "-");
    sb.insert(23, "-");

    String temp = sb.toString().toUpperCase();
    formatter.close();
    return temp;
}

From source file:Main.java

static public String pathTo(Node xmlNode) {
    StringBuilder result = new StringBuilder();

    for (Node node = xmlNode; node != null; node = node.getParentNode()) {
        if (node.getNodeType() == Node.DOCUMENT_NODE)
            result.insert(0, node.getNodeName());
        else/*from  w  w w  . j a  v a 2 s  .c o  m*/
            result.insert(0, " -> " + node.getNodeName());
    }

    return result.toString();
}

From source file:Main.java

/**
 * Format size in string form//from ww w.ja va 2s. c om
 *
 * @param size Size in bytes
 * @return Size in stinrg format with suffix
 */
public static String formatSize(int size) {
    String suffix = null;

    if (size >= 1024) {
        suffix = "KB";
        size /= 1024;
        if (size >= 1024) {
            suffix = "MB";
            size /= 1024;
        }
    }

    StringBuilder resultBuffer = new StringBuilder(Long.toString(size));

    int commaOffset = resultBuffer.length() - 3;
    while (commaOffset > 0) {
        resultBuffer.insert(commaOffset, ',');
        commaOffset -= 3;
    }

    if (suffix != null)
        resultBuffer.append(suffix);
    return resultBuffer.toString();
}

From source file:com.Axeryok.CocoaInput.CocoaInput.java

public static String formatMarkedText(String aString, int position1, int length1) {
    StringBuilder builder = new StringBuilder(aString);
    if (length1 != 0) {
        builder.insert(position1 + length1, "rn");
        builder.insert(position1, "l");
    }// w ww  .j a  v a  2s  .co m
    builder.insert(0, "n");
    builder.append("r");

    return new String(builder);
}

From source file:edu.rit.flick.genetics.util.HexPrinter.java

public static String intToBinaryString(final int i) {
    final StringBuilder str = new StringBuilder(
            org.apache.commons.lang.StringUtils.leftPad(Integer.toBinaryString(i), 16, '0'));

    for (int idx = str.length() - 4; idx > 0; idx -= 4)
        str.insert(idx, "_");

    return str.toString();
}

From source file:Main.java

/**
 * Get characters' unicode in hexadecimal.
 *//*w  w  w.j a  v  a  2s .  c o m*/
public static String getUnicode(String text, int len_limit) {
    if (TextUtils.isEmpty(text) || len_limit <= 0) {
        return "";
    }

    StringBuilder stringBuilder = new StringBuilder();

    final char[] CHARS = text.toCharArray();
    for (int len = CHARS.length, i = len - 1; i >= 0; --i) {
        if (len - i <= len_limit) {
            stringBuilder.insert(0, Integer.toHexString(CHARS[i]).toUpperCase());
            stringBuilder.insert(0, " ");
        } else {
            stringBuilder.insert(0, " ...");
            break;
        }
    }

    //Remove the first superfluous " ".
    return stringBuilder.substring(1);
}

From source file:Main.java

private static void insertT9Key(@NonNull StringBuilder t9KeyBuilder, @NonNull String t9Str) {
    if (t9Str.length() == 0)
        return;/*from  w w w  . ja  v  a2 s .  co m*/

    int index = -1;
    while ((index = t9KeyBuilder.indexOf(String.valueOf(T9_KEYS_DIVIDER), index + 1)) >= 0) {
        t9KeyBuilder.insert(index, t9Str);
        index += t9Str.length();
    }
}

From source file:Main.java

public static String formatSize(long size) {
    String suffix = null;/*  w w  w.  j  a  v a2 s.c o m*/

    if (size >= 1024) {
        suffix = "KB";
        size /= 1024;
        if (size >= 1024) {
            suffix = "MB";
            size /= 1024;
        }
    }

    StringBuilder resultBuffer = new StringBuilder(Long.toString(size));

    int commaOffset = resultBuffer.length() - 3;
    while (commaOffset > 0) {
        resultBuffer.insert(commaOffset, ',');
        commaOffset -= 3;
    }

    if (suffix != null) {
        resultBuffer.append(suffix);
    }

    return resultBuffer.toString();
}