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

后端 未结 8 1645
广开言路
广开言路 2020-11-30 04:39

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

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 04:57

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

提交回复
热议问题