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

JavaStringCharacter EncodingChar

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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.

Solution 3 - Java

There is many ways to do that, but the simplest seems to be the following one:

boolean isUpperCase = Character.isUpperCase("My String".charAt(0));

Solution 4 - Java

Don't forget to check whether the string is empty or null. If we forget checking null or empty then we would get NullPointerException or StringIndexOutOfBoundException if a given String is null or empty.

public class StartWithUpperCase{
    	
    	public static void main(String[] args){
    
    		String str1 = ""; //StringIndexOfBoundException if 
                              //empty checking not handled
    		String str2 = null; //NullPointerException if 
                                //null checking is not handled.
    		String str3 = "Starts with upper case";
    		String str4 = "starts with lower case";
    
    		System.out.println(startWithUpperCase(str1)); //false
    		System.out.println(startWithUpperCase(str2)); //false
    		System.out.println(startWithUpperCase(str3)); //true
    		System.out.println(startWithUpperCase(str4)); //false
    
    
    
    	}
    
    	public static boolean startWithUpperCase(String givenString){
    
    		if(null == givenString || givenString.isEmpty() ) return false;
    		else return (Character.isUpperCase( givenString.codePointAt(0) ) );
    	}
    
    }

Solution 5 - Java

Make sure you first check for null and empty and ten converts existing string to upper. Use S.O.P if want to see outputs otherwise boolean like Rabiz did.

 public static void main(String[] args)
 {
     System.out.println("Enter name");
     Scanner kb = new Scanner (System.in);
     String text =  kb.next();

     if ( null == text || text.isEmpty())
     {
         System.out.println("Text empty");
     }
     else if (text.charAt(0) == (text.toUpperCase().charAt(0)))
     {
         System.out.println("First letter in word "+ text + " is upper case");
     }
  }

Solution 6 - Java

If you have to check it out manually you can do int a = s.charAt(0)

If the value of a is between 65 to 90 it is upper case.

Solution 7 - Java

String yourString = "yadayada";
if (Character.isUpperCase(yourString.charAt(0))) {
    // print something
} else {
    // print something else
}

Solution 8 - Java

we can find upper case letter by using regular expression as well

private static void findUppercaseFirstLetterInString(String content) {
	Matcher m = Pattern
			.compile("([a-z])([a-z]*)", Pattern.CASE_INSENSITIVE).matcher(
					content);
	System.out.println("Given input string : " + content);
	while (m.find()) {
		if (m.group(1).equals(m.group(1).toUpperCase())) {
			System.out.println("First Letter Upper case match found :"
					+ m.group());
		}
	}
}

for detailed example . please visit http://www.onlinecodegeek.com/2015/09/how-to-determines-if-string-starts-with.html

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVjyView Question on Stackoverflow
Solution 1 - JavavitautView Answer on Stackoverflow
Solution 2 - JavadivecView Answer on Stackoverflow
Solution 3 - JavaCrozinView Answer on Stackoverflow
Solution 4 - JavaRazibView Answer on Stackoverflow
Solution 5 - JavaYoko AlphaView Answer on Stackoverflow
Solution 6 - Javauser506710View Answer on Stackoverflow
Solution 7 - JavaOm PrakashView Answer on Stackoverflow
Solution 8 - JavaThulasiramView Answer on Stackoverflow