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 void main(String[] a) {
    StringBuilder builder = new StringBuilder("Line 1\n");

    builder.append("Line 3\n");

    String lineToInsert = "Line 2\n";

    builder.insert(0, lineToInsert);

    System.out.println(builder.toString());

}

From source file:Main.java

public static void main(String[] args) {
    // Create an empty StringBuffer
    StringBuilder sb = new StringBuilder();
    printDetails(sb);/*from   w ww .ja v a2 s.  co  m*/

    // Append "good"
    sb.append("good");
    printDetails(sb);

    // Insert "Hi " in the beginning
    sb.insert(0, "Hi ");
    printDetails(sb);

    // Delete the first o
    sb.deleteCharAt(1);
    printDetails(sb);

    // Append "  be  with  you"
    sb.append(" be  with  you");
    printDetails(sb);

    // Set the length to 3
    sb.setLength(3);
    printDetails(sb);

    // Reverse the content
    sb.reverse();
    printDetails(sb);
}

From source file:ComputeResult.java

public static void main(String[] args) {
    String original = "software";
    StringBuilder result = new StringBuilder("hi");
    int index = original.indexOf('a');

    /*1*/ result.setCharAt(0, original.charAt(0));
    /*2*/ result.setCharAt(1, original.charAt(original.length() - 1));
    /*3*/ result.insert(1, original.charAt(4));
    /*4*/ result.append(original.substring(1, 4));
    /*5*/ result.insert(3, (original.substring(index, index + 2) + " "));

    System.out.println(result);//from w w  w.  j  av  a 2 s.  c  o m
}

From source file:org.eclipse.swt.snippets.Snippet179.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 179");
    shell.setLayout(new GridLayout());
    final Text text = new Text(shell, SWT.BORDER);
    text.setText("YYYY/MM/DD");
    final Calendar calendar = Calendar.getInstance();
    text.addListener(SWT.Verify, new Listener() {
        boolean ignore;

        @Override/*w ww  .j av a 2 s.  c om*/
        public void handleEvent(Event e) {
            if (ignore)
                return;
            e.doit = false;
            StringBuilder buffer = new StringBuilder(e.text);
            char[] chars = new char[buffer.length()];
            buffer.getChars(0, chars.length, chars, 0);
            if (e.character == '\b') {
                for (int i = e.start; i < e.end; i++) {
                    switch (i) {
                    case 0: /* [Y]YYY */
                    case 1: /* Y[Y]YY */
                    case 2: /* YY[Y]Y */
                    case 3: /* YYY[Y] */ {
                        buffer.append('Y');
                        break;
                    }
                    case 5: /* [M]M*/
                    case 6: /* M[M] */ {
                        buffer.append('M');
                        break;
                    }
                    case 8: /* [D]D */
                    case 9: /* D[D] */ {
                        buffer.append('D');
                        break;
                    }
                    case 4: /* YYYY[/]MM */
                    case 7: /* MM[/]DD */ {
                        buffer.append('/');
                        break;
                    }
                    default:
                        return;
                    }
                }
                text.setSelection(e.start, e.start + buffer.length());
                ignore = true;
                text.insert(buffer.toString());
                ignore = false;
                text.setSelection(e.start, e.start);
                return;
            }

            int start = e.start;
            if (start > 9)
                return;
            int index = 0;
            for (int i = 0; i < chars.length; i++) {
                if (start + index == 4 || start + index == 7) {
                    if (chars[i] == '/') {
                        index++;
                        continue;
                    }
                    buffer.insert(index++, '/');
                }
                if (chars[i] < '0' || '9' < chars[i])
                    return;
                if (start + index == 5 && '1' < chars[i])
                    return; /* [M]M */
                if (start + index == 8 && '3' < chars[i])
                    return; /* [D]D */
                index++;
            }
            String newText = buffer.toString();
            int length = newText.length();
            StringBuilder date = new StringBuilder(text.getText());
            date.replace(e.start, e.start + length, newText);
            calendar.set(Calendar.YEAR, 1901);
            calendar.set(Calendar.MONTH, Calendar.JANUARY);
            calendar.set(Calendar.DATE, 1);
            String yyyy = date.substring(0, 4);
            if (yyyy.indexOf('Y') == -1) {
                int year = Integer.parseInt(yyyy);
                calendar.set(Calendar.YEAR, year);
            }
            String mm = date.substring(5, 7);
            if (mm.indexOf('M') == -1) {
                int month = Integer.parseInt(mm) - 1;
                int maxMonth = calendar.getActualMaximum(Calendar.MONTH);
                if (0 > month || month > maxMonth)
                    return;
                calendar.set(Calendar.MONTH, month);
            }
            String dd = date.substring(8, 10);
            if (dd.indexOf('D') == -1) {
                int day = Integer.parseInt(dd);
                int maxDay = calendar.getActualMaximum(Calendar.DATE);
                if (1 > day || day > maxDay)
                    return;
                calendar.set(Calendar.DATE, day);
            } else {
                if (calendar.get(Calendar.MONTH) == Calendar.FEBRUARY) {
                    char firstChar = date.charAt(8);
                    if (firstChar != 'D' && '2' < firstChar)
                        return;
                }
            }
            text.setSelection(e.start, e.start + length);
            ignore = true;
            text.insert(newText);
            ignore = false;
        }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:Main.java

public static String formatMac(String mac) {
    StringBuilder sb = new StringBuilder(mac);
    sb.insert(2, ":");
    sb.insert(5, ":");
    sb.insert(8, ":");
    sb.insert(11, ":");
    sb.insert(14, ":");
    return sb.toString();
}

From source file:Main.java

public static String prettifyFingerprint(String fingerprint) {
    StringBuilder builder = new StringBuilder(fingerprint);
    builder.insert(8, " ");
    builder.insert(17, " ");
    builder.insert(26, " ");
    builder.insert(35, " ");
    return builder.toString();
}

From source file:Main.java

public static String processKissMetricsQuery(String query, StringBuilder args) {
    if (args.length() > 0) {
        args.insert(0, URL_AND);
    }/*ww  w .  j  a v  a 2s.  c  o m*/
    args.insert(0, query);
    return args.toString();
}

From source file:Main.java

public static byte[] hexStringToByteArray(String s) {
    if (s.length() % 2 != 0) {
        StringBuilder stringBuilder = new StringBuilder(s);
        stringBuilder.insert(s.length() - 1, "0");
        s = stringBuilder.toString();/*from ww  w .ja  va  2  s. c  o  m*/
    }

    int len = s.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
    }
    return data;
}

From source file:Main.java

public static String getMoneyString(int value) {
    String returnValue = "" + value;
    StringBuilder builder = new StringBuilder(returnValue);
    if (returnValue.length() > 2) {
        builder.insert(builder.length() - 2, ".");
        builder.insert(0, "$");
    } else {//from   w w w.j  a va2  s.  c  o m
        builder.insert(0, "$0.");
    }

    return builder.toString();
}

From source file:Main.java

/**
 * Convert hex string to byte[]//from   w w w  . j  a va2 s.  c  o m
 *
 * @param hexString the hex string
 * @return byte[]
 */
public static byte[] hexStringToBytes(String hexString) {
    if (hexString == null || hexString.equals("")) {
        return null;
    }

    if (hexString.length() % 2 != 0) {
        StringBuilder stringBuilder = new StringBuilder(hexString);
        stringBuilder.insert(hexString.length() - 1, "0");
        hexString = stringBuilder.toString();
        System.out.println("new hex string------------------->" + hexString);
    }

    hexString = hexString.toUpperCase();
    int length = hexString.length() / 2;
    char[] hexChars = hexString.toCharArray();
    byte[] d = new byte[length];
    for (int i = 0; i < length; i++) {
        int pos = i * 2;
        d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
    }
    return d;
}