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)

Parameters:

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

Returns:

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.

Since:

1.5

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

package com.ehowtonow.java.lang.string;

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);
		
		
	
	}

}

Output

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(String.java:717)
at com.ehowtonow.java.lang.string.CodePointBeforeExample.main(CodePointBeforeExample.java:21)


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(int ch) method with example
  2. How to get Character unicode point in a String
  3. How to Split the String in Java
  4. How to find last occurrence of substring inside string
  5. Java String compareTo() method with example
  6. Java String Compare
  7. Java String indexOf(String str, int fromIndex) method with example
  8. Java String codePointBefore() method with example
  9. Java String indexOf(String str) method with example
  10. Java String concat() method with example
  11. Java String indexOf(int ch, int fromIndex) method with example
  12. How to convert a String to Upper case
  13. Java String compareToIgnoreCase() method with example
  14. How to search a word inside String
  15. Java String charAt() method with example

Be the first to comment

Leave a Reply

Your email address will not be published.


*