In this article, we will discuss how to get a specific character from the supplied/passed string content
1. String’s charAt(int index) method:
- This String method returns the character value at the specified index position (by invoking from string value/content)
1.1 Method Signature:
public char charAt(int index);
1.2 Returns:
- Returns the char value at the specified index
1.3 Throws:
- String’s charAt() method throws IndexOutOfBoundsException, if index value supplied/passed falls out of range
- IndexOutOfBoundsException is thrown, if the input index value is out of range i.e.;
- Index position is negative (<0)
- Index position is greater than length()-1
2. Examples on charAt() method:
2.1 Extracting or getting character value at specified index position
StringCharAtMethod.java
package in.bench.resources.string.methods;
public class StringCharAtMethod {
public static void main(String[] args) {
// sample string to get char at specified index
String strSample = "BenchResources.Net";
// returns character value at 5th index position
char charAt1 = strSample.charAt(5);
// printing to the console
System.out.println("The character at 5th index-position"
+ " is: " + charAt1);
// returns character value at 15th index position
char charAt2 = strSample.charAt(15);
// printing to the console
System.out.println("The character at 15th index-position"
+ " is: " + charAt2);
}
}
Output:
The character at 5th index position is: R
The character at 15th index position is: N
2.2 Exception scenario by specifying index out of range
StringCharAtMethod.java
package in.bench.resources.string.methods;
public class StringCharAtMethod {
public static void main(String[] args) {
// sample string to get char at specified index
String strSample = "BenchResources.Net";
// returns character value at 18th index position
char charAt1 = strSample.charAt(18);
// printing to the console
System.out.println("The character at 18th index position "
+ "is: " + charAt1);
}
}
Output:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: 18
at java.lang.String.charAt(Unknown Source)
at in.bench.resources.override.tostring.StringCharAtMethod
.main(StringCharAtMethod.java:11
Related Articles:
- Java – String charAt(int index) method
- Java – String compareTo(String anotherString) method
- Java – String compareToIgnoreCase(String str) method
- Java – String concat(String str) method
- Java – String contains(CharSequence s) method
- Java – String contentEquals(StringBuffer sb) method
- Java – String copyValueOf(char[] data) method (2)
- Java – String endsWith(String suffix) method
- Java – String equals(Object anObject) method
- Java – String equalsIgnoreCase(Object anObject) method
- Java – String format(String format, Object… args) method
- Java – String getBytes() method (4)
- Java – String getChars() method
- Java – String hashCode() method
- Java – String indexOf() method (4)
- Java – String intern() method
- Java – String isEmpty() method
- Java – String join() method (2)
- Java – String lastIndexOf() method (4)
- Java – String length() method
- Java – String matches(String regex) method
- Java – String regionMatches() method (2)
- Java – String replace(char oldChar, char newChar) method
- Java – String replace(CharSequence target, CharSequence replacement) method
- Java – String replaceAll(String regex, String replacement) method
- Java – String replaceFirst(String regex, String replacement) method
- Java – String split(String regex) method
- Java – String split(String regex, int limit) method
- Java – String startsWith(String prefix) method
- Java – String startsWith(String prefix, int toffset) method
- Java – String subSequence(int beginIndex, int endIndex) method
- Java – String substring(int beginIndex) method
- Java – String substring(int beginIndex, int endIndex) method
- Java – String toCharArray() method
- Java – String toLowerCase() method (2)
- Java – String toUpperCase() method (2)
- Java – String toString() method
- Java – String trim() method
- Java – String valueOf() method (9)
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 !!