Java String codePointBefore() method with example

Java String codePointBefore() method Returns the character (Unicode code point) before the specified index. The index refers to char values (Unicode code units) and ranges from 1 to length. If the char value at (index – 1) is in the low-surrogate range, (index – 2) is not negative, and the char value at (index – 2) is in the high-surrogate range, then the supplementary code point value of the surrogate pair is returned. If the char value at index – 1 is an unpaired low-surrogate or a high-surrogate, the surrogate value is returned.

Method Signature

public int codePointBefore(int index)


index – the index following the code point that should be returned


the Unicode code point value before the given index.

Throws Exception:

IndexOutOfBoundsException – if the index argument is less than 1 or greater than the length of this string.



String codePointBefore() method Implementation

public int codePointBefore(int index) {
        int i = index - 1;
        if ((i < 0) || (i >= value.length)) {
            throw new StringIndexOutOfBoundsException(index);
        return Character.codePointBeforeImpl(value, index, 0);

Java String codePointBefore() method example


public class CodePointBeforeExample {

	public static void main(String[] args) {

		String s1="ehowtonow";  
		System.out.println("String : "+s1);
		int codePointBefore = s1.codePointBefore(1);
		System.out.println("codePointBefore 1 : "+codePointBefore);
		codePointBefore = s1.codePointBefore(7);
		System.out.println("codePointBefore 7 : "+codePointBefore);
		System.out.println("codePointBefore 0 & 10 will throw IndexOutOfBoundsException ");
		codePointBefore = s1.codePointBefore(10);
		codePointBefore = s1.codePointBefore(0);



String : ehowtonow
codePointBefore 1 : 101
codePointBefore 7 : 110
codePointBefore 0 & 10 will throw IndexOutOfBoundsException
Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 10
at java.lang.String.codePointBefore(

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 compareToIgnoreCase() method with example
  2. Java String indexOf(String str) method with example
  3. How to find last occurrence of substring inside string
  4. Java String codePointAt() method with example
  5. How to convert a String to Lower case
  6. How to format the String by using Locale
  7. Java String contentEquals(StringBuffer sb) method with example
  8. Java String indexOf(int ch, int fromIndex) method with example
  9. Java String charAt() method with example
  10. How to get Character unicode point in a String
  11. Remove the particular character from string
  12. Java String Compare
  13. Java String codePointCount() method with example
  14. Java String equals() method with example
  15. Java String contentEquals(CharSequence cs) method with example

Be the first to comment

Leave a Reply