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?
Asked
Active
Viewed 6.5k times
13
-
Upper case? Lower case? Either? – Mark Byers Dec 16 '11 at 21:00
-
If you'll need to match both upper case and lower case characters, check out my post. Mark Byers matches only uppercase and we have a few posts matching lower case, just pick one (and update your question-post to clarify which one it is that you want). – Filip Roséen - refp Dec 16 '11 at 21:04
6 Answers
37
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].*$")) { ... }

Mark Byers
- 811,555
- 193
- 1,581
- 1,452
-
Okay, this worked. but what exactly does the ^ mean, and .*$ mean? I know the [a-i] means a through i – Archey Dec 16 '11 at 21:09
-
i mean, im asking what the ^ and .*$ means, and why i need it. I like to understand everything, as we haven't learned this in my computer science class yet – Archey Dec 16 '11 at 21:20
-
3@Archey The means start of string, the . means any character, * means any number of times and $ is end of line. So, basically start of line followed by A-I followed by any character 0 or more times until the end of line. – Roger Lindsjö Dec 16 '11 at 21:20
-
1@Archey you can accept his answer if you want by clicking the check mark next to the two up and down arrows – D3_JMultiply Dec 16 '11 at 22:15
3
if ( string.charAt(0) >= 'A' && string.charAt(0) <= 'I' )
{
}
should do it

parapura rajkumar
- 24,045
- 1
- 55
- 85
3
How about this for brevity?
if (0 <= "ABCDEFGHI".indexOf(string.charAt(0))) {
// string starts with a character between 'A' and 'I' inclusive
}

rsp
- 23,135
- 6
- 55
- 69
-
-
The above condition is filled, indexOf will return a value greater than -1 if the first character is found within the string. Right now it will act as if all letters accept BCDEGHI is correct. – Filip Roséen - refp Dec 17 '11 at 04:08
-
@Bill, this method obviously isn't the best solution for a range check, for a set of characters, like "AEOIU", it works quite well. – rsp Dec 17 '11 at 09:17
-
@refp, I think you will find the code correct including the `'A'`, `(0 <= 0) == true` :-) – rsp Dec 17 '11 at 09:19
1
char c=string.toLowerCase().charAt(0);
if( c >= 'a' && c <= 'i' )
...
This makes it easy to extract it as a method:
public static boolean startsBetween(String s, char lowest, char highest) {
char c=s.charAt(0);
c=Character.toLowerCase(c); //thx refp
return c >= lowest && c <= highest;
}
which is HIGHLY preferred to any inline solution. For the win, tag it as final so java inlines it for you and gives you better performance than a coded-inline solution as well.

Bill K
- 62,186
- 18
- 105
- 157
-
you shouldn't be lowercasing the entire string, check out how I solved it in my post, yes.. I know; premature optimization ;-) – Filip Roséen - refp Dec 16 '11 at 21:06
-
You are correct, it also copies the whole thing. As long as it's broken out into a method, might as well take the step to break it out into it's own variable. – Bill K Dec 16 '11 at 23:50
0
if ( string.toUpperCase().charAt(0) >= 'A' && string.toUpperCase().charAt(0) <= 'I' )
should be the easiest version...

Sebastian Oberste-Vorth
- 979
- 15
- 31