In this article, we will discuss StringBuffer’s charAt() method which returns character at a specified index-position
1. String’s charAt(int index) method:
- This method returns single character at the specified index-position
1.1 Method Signature:
public char charAt(int index);
1.2 Returns:
- Return char-value at the specified index-position from the invoking StringBuffer object
1.3 Throws:
- IndexOutOfBoundsException is thrown, if the input index value is out of range i.e.;
- Index-position is negative (<0)
- Index-position is greater than StringBuffer length()
2. Examples on charAt() method:
- To get char-value at specified index from invoking StringBuffer
StringBufferCharAtMethod.java
package in.bench.resources.stringbuffer.methods;
public class StringBufferCharAtMethod {
public static void main(String[] args) {
// StringBuffer
StringBuffer sb = new StringBuffer("BenchResources.Net");
// return character value at 5th index position
char charAt1 = sb.charAt(5);
// print to console
System.out.println("1. StringBuffer character "
+ "at 5th index-position is : " + charAt1);
// return character value at 15th index position
char charAt2 = sb.charAt(15);
// print to console
System.out.println("2. StringBuffer character "
+ "at 15th index-position is : " + charAt2);
}
}
Output:
1. StringBuffer character at 5th index-position is : R
2. StringBuffer character at 15th index-position is : N
Related Articles:
- StringBuffer class
- StringBuffer append() method (13)
- StringBuffer capacity() method
- StringBuffer charAt(int index) method
- StringBuffer delete(int start, int end) method
- StringBuffer deleteCharAt(int index) method
- StringBuffer ensureCapacity(int minimumCapacity) method
- StringBuffer indexOf() method (2)
- StringBuffer insert() method (12)
- StringBuffer lastIndexOf() method (2)
- StringBuffer length() method
- StringBuffer replace(int start, int end, String str) method
- StringBuffer reverse() method
- StringBuffer substring(int start, int end) method
References:
- https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
- https://docs.oracle.com/javase/tutorial/java/data/strings.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html
Happy Coding !!
Happy Learning !!