Java String codePointAt() method with example

Java String codePointAt() method returns the character (Unicode code point) at the specified index. The index refers to char values (Unicode code units) and ranges from 0 to length()- 1.

If the char value specified at the given index is in the high-surrogate range, the following index is less than the length of this String, and the char value at the following index is in the low-surrogate range, then the supplementary code point corresponding to this surrogate pair is returned. Otherwise, the char value at the given index is returned.

Method Signature

public int codePointAt(int index)

Parameters:

index – the index to the char values

Returns:

the code point value of the character at the index

Throws Exception:

IndexOutOfBoundsException – if the index argument is negative or not less than the length of this string.

Since:

1.5

String codePointAt() method Implementation

 public int codePointAt(int index) {
        if ((index < 0) || (index >= value.length)) {
            throw new StringIndexOutOfBoundsException(index);
        }
        return Character.codePointAtImpl(value, index, value.length);
    }

Java String codePointAt() method example

package com.ehowtonow.java.lang.string;

public class CodePointAtExample {

	public static void main(String[] args) {
		String s1="ehowtonow";  
		
		System.out.println("String : "+s1);
		
		int codePoint = s1.codePointAt(1);
		
		System.out.println("codePoint At 1 : "+codePoint);
		
		codePoint = s1.codePointAt(7);
		
		System.out.println("codePoint At 7 : "+codePoint);
		
		System.out.println("codePoint At 10 will throw IndexOutOfBoundsException ");
		
		codePoint = s1.codePointAt(10);
		
		
	}

}

Output

String : ehowtonow
codePoint At 1 : 104
codePoint At 7 : 111
codePoint At 10 will throw IndexOutOfBoundsException
Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 10
at java.lang.String.codePointAt(String.java:687)
at com.ehowtonow.java.lang.string.CodePointAtExample.main(CodePointAtExample.java:20)


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. Java String indexOf(String str, int fromIndex) method with example
  2. Java String indexOf(String str) method with example
  3. Java String indexOf(int ch, int fromIndex) method with example
  4. Java String compareToIgnoreCase() method with example
  5. String Concatenation in Java
  6. How to convert a String to Lower case
  7. Java String Compare
  8. Remove the particular character from string
  9. How to Split the String in Java
  10. Java String codePointBefore() method with example
  11. How to get Character unicode point in a String
  12. How to convert a String to Upper case
  13. Java String compareTo() method with example
  14. Java String codePointAt() method with example
  15. How to search a word inside String

Be the first to comment

Leave a Reply

Your email address will not be published.


*