Determine if string starts with letters A through I

前端 未结 6 859
北海茫月
北海茫月 2021-01-11 15:30

I\'ve got a simple java assignment. I need to determine if a string starts with the letter A through I. I know i have to use string.startsWith(); but I don\'t want to write,

6条回答
  •  盖世英雄少女心
    2021-01-11 16:22

    You don't need regular expressions for this.

    Try this, assuming you want uppercase only:

    char c = string.charAt(0);
    if (c >= 'A' && c <= 'I') { ... }
    

    If you do want a regex solution however, you can use this (ideone):

    if (string.matches("^[A-I].*$")) { ... }
    

提交回复
热议问题