在Java中,如何在没有正则表达式的情况下查找字符串中的第一个字符是否为大写

问题描述 投票:0回答:8

在Java中,不使用正则表达式来查找字符串中的第一个字符是否为大写。

java string character-encoding char
8个回答
132
投票

假设

s
非空:

Character.isUpperCase(s.charAt(0))

或者,正如 divec 所提到的,使其适用于具有以上代码点的字符

U+FFFF
:

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

44
投票

实际上,这比看起来更微妙。

对于代码点高于 U+FFFF 的小写字符(例如 U+1D4C3、数学脚本小 N),上面的代码会给出错误的答案。 String.charAt 将返回一个 UTF-16 代理对,可以这么说,它不是一个字符,而是字符的一半。所以你必须使用 String.codePointAt,它返回一个大于 0xFFFF 的 int (不是 char)。你会这样做:

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

不要因为忽略了这一点而感到难过;几乎所有 Java 编码人员都不能很好地处理 UTF-16,因为该术语会误导您认为每个“char”值代表一个字符。 UTF-16 很糟糕,因为它几乎是固定宽度,但又不完全是。因此,非固定宽度的边缘情况往往不会得到测试。直到有一天,出现了一些包含 U+1D4C3 这样的字符的文档,然后你的整个系统就崩溃了。


6
投票

有很多方法可以做到这一点,但最简单的似乎是以下一种:

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

2
投票

不要忘记检查字符串是否为空或

null
。如果我们忘记检查
null
或空,那么如果给定的字符串为 null 或空,我们将得到
NullPointerException
StringIndexOutOfBoundException

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

    }

1
投票

确保首先检查 null 和empty,然后 10 将现有字符串转换为大写。如果想像 Rabiz 那样查看其他布尔值的输出,请使用 S.O.P。

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

0
投票

如果您必须手动检查,您可以这样做

int a = s.charAt(0)

如果 a 的值在 65 到 90 之间,则为大写。


0
投票

我们也可以使用正则表达式找到大写字母

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

查看详细示例。请访问 http://www.onlinecodegeek.com/2015/09/how-to-determines-if-string-starts-with.html


0
投票
String yourString = "yadayada";
if (Character.isUpperCase(yourString.charAt(0))) {
    // print something
} else {
    // print something else
}
© www.soinside.com 2019 - 2024. All rights reserved.