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

后端 未结 8 1625
广开言路
广开言路 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 05:16

    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));
    
    0 讨论(0)
  • 2020-11-30 05:16

    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

    0 讨论(0)
提交回复
热议问题