Char index

ReturnMethodSummary
charcharAt(int index)Returns the char value in this sequence at the specified index.
voidsetCharAt(int index, char ch)The character at the specified index is set to ch.
voidgetChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)Characters are copied from this sequence into the destination character array dst.
intindexOf(String str)Returns the index within this string of the first occurrence of the specified substring.
intindexOf(String str, int fromIndex)Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
intlastIndexOf(String str)Returns the index within this string of the rightmost occurrence of the specified substring.
intlastIndexOf(String str, int fromIndex)Returns the index within this string of the last occurrence of the specified substring.

public class Main {
  public static void main(String[] argv) {
    StringBuffer sb = new StringBuffer();
    sb.append("java2s.com");
    
    char ch =  sb.charAt(0);
    
    System.out.println(ch);

  }
}

The output:


j

The following example demonstrates charAt( ) and setCharAt( ):

 
public class Main {
  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');
    sb.setLength(2);
    System.out.println("buffer after = " + sb);
    System.out.println("charAt(1) after = " + sb.charAt(1));

  }
}

The output:


buffer before = java2s.com
charAt(1) before = a
buffer after = ji
charAt(1) after = i
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.