Is there any advantage to using String.offsetByCodePoints instead of just using an integer index to keep track of where you are in a string?
Is there any advantage to using String.offsetByCodePoints instead of just using an integer index
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It might be useful if the string contains characters from the Unicode Supplementary Planes (unusual characters with a high code point / character code). Java strings use UTF-16 encoding internally, which means that some Unicode characters must be represented as a sequence of two
charvalues, also known as a surrogate pair. Thus, althoughs.charAt(i)will give you the i‘thcharofs, this might not actually be the i‘th character.s.offsetByCodePoints(0, i)will tell you the index where the i‘th character starts.If you are unfamiliar with some of the terms above, you should read Joel Spolsky’s excellent article on character sets.