确定字符串是否以字母A到I开头 [英] Determine if string starts with letters A through I

查看:221
本文介绍了确定字符串是否以字母A到I开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的java分配。我需要确定一个字符串是否以字母A到I开头。我知道我必须使用string.startsWith();但我不想写, if(string.startsWith(a)); 一直到我,看起来效率很高。我应该使用某种循环吗?

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, if(string.startsWith("a")); all the way to I, it seems in efficient. Should I be using a loop of some sort?

推荐答案

你不需要正则表达式。

试试这个,假设你只想要大写:

Try this, assuming you want uppercase only:

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

如果您确实需要正则表达式解决方案,可以使用此方法( ideone ) :

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

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

这篇关于确定字符串是否以字母A到I开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆