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:MainClass.java

public static void main(String args[]) {
    StringBuffer sb = new StringBuffer("This is a test.");

    sb.replace(5, 7, "was");
    System.out.println("After replace: " + sb);
}

From source file:Main.java

public static void main(String args[]) {
    StringBuffer sb = new StringBuffer("This is a test from java2s.com.");

    sb.replace(5, 7, "was");
    System.out.println("After replace: " + sb);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    StringBuffer buf = new StringBuffer("Java this is a test");

    int start = 27;
    int end = 28;
    buf.replace(start, end, "4"); // Java Developers v1.4
    System.out.println(buf);/* w  ww  . ja  v a2s.  co m*/
}

From source file:MainClass.java

public static void main(String[] arg) {
    StringBuffer phrase = new StringBuffer("one two three four");
    String substring = "two";
    String replacement = "twenty";
    int position = phrase.lastIndexOf(substring); // Find start of "two"
    phrase.replace(position, position + substring.length(), replacement);

    System.out.println(phrase);/* ww  w . jav a2  s.c o  m*/
}

From source file:TextVerifyInputFormatDate.java

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    final Text text = new Text(shell, SWT.BORDER);
    text.setText("YYYY/MM/DD");
    ;/*w  ww .  j ava 2  s  . co  m*/
    final Calendar calendar = Calendar.getInstance();
    text.addListener(SWT.Verify, new Listener() {
        boolean ignore;

        public void handleEvent(Event e) {
            if (ignore)
                return;
            e.doit = false;
            StringBuffer buffer = new StringBuffer(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();
            StringBuffer date = new StringBuffer(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

private static String remove(final String str, final String toRemove, final int location) {

    StringBuffer buffer = new StringBuffer(str);
    return buffer.replace(location, location + toRemove.length(), "").toString();

}

From source file:Main.java

public static String formatAccountNo(String accountNo) {
    try {/*ww  w .  ja  va 2s  .c  om*/
        StringBuffer s = new StringBuffer();
        for (int i = 0; i < accountNo.length() - 10; i++) {
            s.append("*");
        }

        StringBuffer sb = new StringBuffer(accountNo);
        sb.replace(6, accountNo.length() - 4, s.toString());
        return sb.toString();
    } catch (Exception e) {
        return accountNo;
    }
}

From source file:Main.java

private static String remove(String str, String toRemove, int location) {

    StringBuffer buffer = new StringBuffer(str);
    return buffer.replace(location, location + toRemove.length(), "").toString();

}

From source file:Main.java

public static String subTel(String number) {
    if (number != null && number.length() == 11) {
        StringBuffer buffer = new StringBuffer(number);
        StringBuffer replace = buffer.replace(3, 7, "****");
        return replace.toString();
    }// www  .  j a  v  a2 s  . c o  m
    return null;
}

From source file:Main.java

public static String convertListToURLParamsString(List<?> values) {
    StringBuffer buff = new StringBuffer();
    for (Object object : values) {
        buff.append(object.toString() + "&");
    }/*from  w  w  w .  j a v a2s .  com*/
    buff.replace(buff.length() - 1, buff.length(), "");
    return buff.toString();
}