Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, how to find if first character in a string is upper case without regex

In Java, find if the first character in a string is upper case without using regular expressions.

like image 516
Vjy Avatar asked Dec 15 '10 17:12

Vjy


People also ask

How do you check if the first character of a string is uppercase in Java?

isUpperCase(char ch) determines if the specified character is an uppercase character. A character is uppercase if its general category type, provided by Character.

How do you check if a character in a string is uppercase?

To check if a letter in a string is uppercase or lowercase use the toUpperCase() method to convert the letter to uppercase and compare it to itself. If the comparison returns true , then the letter is uppercase, otherwise it's lowercase. Copied!

How do you check if a string is all uppercase Java?

To check if a string contains an uppercase character in Java, call Character. isUpperCase on every character of the string.

How do you compare the first character of a string in Java?

The idea is to use charAt() method of String class to find the first and last character in a string. The charAt() method accepts a parameter as an index of the character to be returned. The first character in a string is present at index zero and the last character in a string is present at index length of string-1 .


2 Answers

Assuming s is non-empty:

Character.isUpperCase(s.charAt(0)) 

or, as mentioned by divec, to make it work for characters with code points above U+FFFF:

Character.isUpperCase(s.codePointAt(0)); 
like image 199
vitaut Avatar answered Sep 18 '22 17:09

vitaut


Actually, this is subtler than it looks.

The code above would give the incorrect answer for a lower case character whose code point was above U+FFFF (such as U+1D4C3, MATHEMATICAL SCRIPT SMALL N). String.charAt would return a UTF-16 surrogate pair, which is not a character, but rather half the character, so to speak. So you have to use String.codePointAt, which returns an int above 0xFFFF (not a char). You would do:

Character.isUpperCase(s.codePointAt(0));

Don't feel bad overlooked this; almost all Java coders handle UTF-16 badly, because the terminology misleadingly makes you think that each "char" value represents a character. UTF-16 sucks, because it is almost fixed width but not quite. So non-fixed-width edge cases tend not to get tested. Until one day, some document comes in which contains a character like U+1D4C3, and your entire system blows up.

like image 25
divec Avatar answered Sep 21 '22 17:09

divec