Determine if string starts with letters A through I
Asked Answered
S

6

13

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?

Seaport answered 16/12, 2011 at 20:55 Comment(2)
Upper case? Lower case? Either?Aminaamine
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).Moriah
A
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].*$")) { ... }
Aminaamine answered 16/12, 2011 at 20:57 Comment(4)
Okay, this worked. but what exactly does the ^ mean, and .*$ mean? I know the [a-i] means a through iSeaport
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 yetSeaport
@Seaport 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.Effortful
@Seaport you can accept his answer if you want by clicking the check mark next to the two up and down arrowsStocktonontees
F
3
if ( string.charAt(0) >= 'A' && string.charAt(0) <= 'I' )
{
}

should do it

Fit answered 16/12, 2011 at 20:56 Comment(0)
U
3

How about this for brevity?

if (0 <= "ABCDEFGHI".indexOf(string.charAt(0))) {
    // string starts with a character between 'A' and 'I' inclusive
}
Unearth answered 16/12, 2011 at 21:8 Comment(4)
Reasonable, now put it into a method and make A and I parameters.Wuhu
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.Moriah
@Bill, this method obviously isn't the best solution for a range check, for a set of characters, like "AEOIU", it works quite well.Unearth
@refp, I think you will find the code correct including the 'A', (0 <= 0) == true :-)Unearth
I
1

Try

string.charAt(0) >= 'a' && string.charAt(0) <= 'j'
Iorgo answered 16/12, 2011 at 21:0 Comment(0)
W
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.

Wuhu answered 16/12, 2011 at 21:0 Comment(2)
you shouldn't be lowercasing the entire string, check out how I solved it in my post, yes.. I know; premature optimization ;-)Moriah
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.Wuhu
N
0

if ( string.toUpperCase().charAt(0) >= 'A' && string.toUpperCase().charAt(0) <= 'I' )

should be the easiest version...

Numbat answered 16/12, 2011 at 21:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.