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?
Determine if string starts with letters A through I
Asked Answered
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
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].*$")) { ... }
Okay, this worked. but what exactly does the ^ mean, and .*$ mean? I know the [a-i] means a through i –
Seaport
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 –
Seaport
@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 arrows –
Stocktonontees
if ( string.charAt(0) >= 'A' && string.charAt(0) <= 'I' )
{
}
should do it
How about this for brevity?
if (0 <= "ABCDEFGHI".indexOf(string.charAt(0))) {
// string starts with a character between 'A' and 'I' inclusive
}
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 Try
string.charAt(0) >= 'a' && string.charAt(0) <= 'j'
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.
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
if ( string.toUpperCase().charAt(0) >= 'A' && string.toUpperCase().charAt(0) <= 'I' )
should be the easiest version...
© 2022 - 2024 — McMap. All rights reserved.