1

This might seem like an already answered question, but I couldn't find it anywhere.

How do I get the first printable character in Java?

For example

abcd    //should return "a" - The first printable char is of 1 bytes
     //should return "" - The first printable char is of 4 bytes (2 codepoints)
"".length() //2 - I guess thats because of 2 codepoints
☠☠     //should return ☠ - The first printable char is of 1 bytes
     //should return  - The first printable char is of 8 bytes (4 codepoints)
abcd   //should return  - The first printable char is of 8 bytes (4 codepoints)
Pankaj Singhal
  • 15,283
  • 9
  • 47
  • 86
  • wouldn't `String::charAt(0)` do the trick? – 0xff Oct 04 '21 at 07:18
  • 1
    no. coz As mentioned in the question, a printable char can be of variable bytes. `String::charAt(0)` returns only 1st byte – Pankaj Singhal Oct 04 '21 at 07:20
  • what about the regex `^.`? – 0xff Oct 04 '21 at 07:26
  • 2
    It's really unclear what exactly you want to know. Getting the first codepoint (which can be two `char` values (not bytes!)) can be done doing `String.codepointAt(0)`. But that doesn't guarantee that what you get is a single complete *user-perceived character*. Please tell us **why** you need this, so we can have a better idea what **exactly** you need. This might end up being an [XY problem](https://xyproblem.info). – Joachim Sauer Oct 04 '21 at 07:30
  • I'm building a game in which I need to show the first characters of player's name representing each player. Currently we are allowing people to enter any type of char into their name field while signing up and restricting that isn't possible right now. (that requires team discussions and larger audience) – Pankaj Singhal Oct 04 '21 at 08:00
  • 2
    That sounds like you really want the first grapheme (which mostly maps onto the naive notion of "first character" that a user sees). That can be multiple codepoints. I think [`BreakIterator.getCharacterInstance(Locale)`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/text/BreakIterator.html#getCharacterInstance(java.util.Locale)) could help. – Joachim Sauer Oct 04 '21 at 09:10
  • This is covering a good number of cases. But this is not working for emojis which need more bytes. eg: - flag of India. The flag of India is of 8 bytes & is represented by these 2 chars ` ` written together. `BreakIterator` returns only `` and doesn't return both together as – Pankaj Singhal Oct 04 '21 at 18:08

0 Answers0