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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4452939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 17:43:41  来源:igfitidea点击:

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

javastringcharacter-encodingchar

提问by Vjy

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

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

采纳答案by vitaut

Assuming sis non-empty:

假设s非空:

Character.isUpperCase(s.charAt(0))

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

或者,如divec所述,使其适用于具有上述代码点的字符U+FFFF

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

回答by Crozin

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

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

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

回答by Crozin

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

如果您必须手动检查它,您可以这样做 int a = s.charAt(0)

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

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

回答by divec

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:

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

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.

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

回答by Om Prakash

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

回答by Thulasiram

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

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

回答by Razib

Don't forget to check whether the string is empty or null. If we forget checking nullor empty then we would get NullPointerExceptionor StringIndexOutOfBoundExceptionif a given String is null or empty.

不要忘记检查字符串是空的还是null. 如果我们忘记检查null或为空,那么我们会得到NullPointerExceptionStringIndexOutOfBoundException给定的字符串是否为空或空。

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

    }

回答by Yoko Alpha

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.

确保您首先检查 null 和 empty 并十次将现有字符串转换为 upper。如果想查看输出,请使用 SOP,否则像 Rabiz 那样是布尔值。

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