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 contentEquals(CharSequence cs) method with example
  2. Java String equals() method with example
  3. Java String indexOf(String str) method with example
  4. Java String equalsIgnoreCase() method with example
  5. Java String endsWith() method with example
  6. Java String indexOf(int ch) method with example
  7. Java String charAt() method with example
  8. String Concatenation in Java
  9. How to format the String by using Locale
  10. How to Split the String in Java
  11. Java String indexOf(int ch, int fromIndex) method with example
  12. How to search a word inside String
  13. How to get Character unicode point in a String
  14. Java String contains() method with example
  15. Java String concat() method with example

Be the first to comment

Leave a Reply