List of usage examples for java.lang StringBuffer setCharAt
@Override public synchronized void setCharAt(int index, char ch)
From source file:MainClass.java
public static void main(String[] arg) { StringBuffer phrase = new StringBuffer("one two three four"); phrase.setCharAt(3, 'Z'); System.out.println(phrase);/*from www . j a v a2s . co m*/ }
From source file:Main.java
public static void main(String[] argv) throws Exception { StringBuffer buf = new StringBuffer("Java this is a test"); int index = 15; buf.setCharAt(index, '.'); System.out.println(buf);//ww w .ja va 2s .c om }
From source file:setCharAtDemo.java
public static void main(String args[]) { StringBuffer sb = new StringBuffer("Hello"); System.out.println("buffer before = " + sb); System.out.println("charAt(1) before = " + sb.charAt(1)); sb.setCharAt(1, 'i'); sb.setLength(2);//from w w w .j a v a 2s .co m System.out.println("buffer after = " + sb); System.out.println("charAt(1) after = " + sb.charAt(1)); }
From source file:Main.java
public static void main(String args[]) { StringBuffer sb = new StringBuffer("java2s.com"); System.out.println("buffer before = " + sb); System.out.println("charAt(1) before = " + sb.charAt(1)); sb.setCharAt(1, 'i'); System.out.println("buffer after = " + sb); System.out.println("charAt(1) after = " + sb.charAt(1)); }
From source file:MainClass.java
public static void main(String args[]) { StringBuffer sb = new StringBuffer("Hello"); System.out.println("buffer before = " + sb); System.out.println("charAt(1) before = " + sb.charAt(1)); sb.setCharAt(1, 'i'); System.out.println("buffer after = " + sb); System.out.println("charAt(1) after = " + sb.charAt(1)); }
From source file:MainClass.java
public static void main(String args[]) { StringBuffer buffer = new StringBuffer("hello there"); System.out.printf("buffer = %s\n", buffer.toString()); System.out.printf("Character at 0: %s\nCharacter at 4: %s\n\n", buffer.charAt(0), buffer.charAt(4)); char charArray[] = new char[buffer.length()]; buffer.getChars(0, buffer.length(), charArray, 0); System.out.print("The characters are: "); for (char character : charArray) System.out.print(character); buffer.setCharAt(0, 'H'); buffer.setCharAt(6, 'T'); System.out.printf("\n\nbuf = %s", buffer.toString()); buffer.reverse();/*from www .j a va 2 s . c om*/ System.out.printf("\n\nbuf = %s\n", buffer.toString()); }
From source file:TextVerifyInputRegularExpression.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); Font font = new Font(display, "Courier New", 10, SWT.NONE); //$NON-NLS-1$ text.setFont(font);/*from w w w . j av a2s .c o m*/ text.setText(template); text.addListener(SWT.Verify, new Listener() { // create the pattern for verification Pattern pattern = Pattern.compile(REGEX); // ignore event when caused by inserting text inside event handler boolean ignore; public void handleEvent(Event e) { if (ignore) return; e.doit = false; if (e.start > 13 || e.end > 14) return; StringBuffer buffer = new StringBuffer(e.text); // handle backspace if (e.character == '\b') { for (int i = e.start; i < e.end; i++) { // skip over separators switch (i) { case 0: if (e.start + 1 == e.end) { return; } else { buffer.append('('); } break; case 4: if (e.start + 1 == e.end) { buffer.append(new char[] { '#', ')' }); e.start--; } else { buffer.append(')'); } break; case 8: if (e.start + 1 == e.end) { buffer.append(new char[] { '#', '-' }); e.start--; } else { buffer.append('-'); } break; default: buffer.append('#'); } } text.setSelection(e.start, e.start + buffer.length()); ignore = true; text.insert(buffer.toString()); ignore = false; // move cursor backwards over separators if (e.start == 5 || e.start == 9) e.start--; text.setSelection(e.start, e.start); return; } StringBuffer newText = new StringBuffer(defaultText); char[] chars = e.text.toCharArray(); int index = e.start - 1; for (int i = 0; i < e.text.length(); i++) { index++; switch (index) { case 0: if (chars[i] == '(') continue; index++; break; case 4: if (chars[i] == ')') continue; index++; break; case 8: if (chars[i] == '-') continue; index++; break; } if (index >= newText.length()) return; newText.setCharAt(index, chars[i]); } // if text is selected, do not paste beyond range of selection if (e.start < e.end && index + 1 != e.end) return; Matcher matcher = pattern.matcher(newText); if (matcher.lookingAt()) { text.setSelection(e.start, index + 1); ignore = true; text.insert(newText.substring(e.start, index + 1)); ignore = false; } } }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } font.dispose(); display.dispose(); }
From source file:Main.java
public static String replaceCharAt(String s, int pos, char c) { StringBuffer buf = new StringBuffer(s); buf.setCharAt(pos, c); return buf.toString(); }
From source file:Main.java
private static StringBuffer swap(StringBuffer s, int a, int b) { char t = s.charAt(a); s.setCharAt(a, s.charAt(b)); s.setCharAt(b, t);//from ww w .j a v a 2s . co m return s; }
From source file:Main.java
/** * Make sure an element/attribute name is a valid NCname. Note that the name is assumed non-qualified, i.e. no * namespaces (hence the Non Colon Name). * //w ww . j a va 2s . com * @param name Proposed non-qualified name for an element/attribute. * @return Same string, with any invalid characters replaced by the underscore '_' character. */ public static String makeValidNCName(String name) { if (name == null || name.length() <= 0) throw new IllegalArgumentException("Invalid NCName: null or empty not allowed!"); if (name.equalsIgnoreCase("XML")) throw new IllegalArgumentException("Invalid NCName: 'XML' name is reserved!"); StringBuffer s = new StringBuffer(name); char c = name.charAt(0); if (!Character.isLetter(c) && c != '_') s.setCharAt(0, '_'); for (int i = 1; i < name.length(); i++) { c = name.charAt(i); if (!Character.isLetter(c) && !Character.isDigit(c) && c != '_' && c != '.' && c != '-') s.setCharAt(i, '_'); } return s.toString(); }