| Author |
Does String.startsWith() support multiple string?
|
Jack Bush
Ranch Hand
Joined: Oct 20, 2006
Posts: 235
|
|
Hi All,
I am currently having to use a number of separate ( String.startsWith("apple") || String.startsWith("orange") || String.startsWith("grape") .....) as opposed to the following regular expression:
Any suggestion?
Thanks a lot,
Jack
|
 |
john price
Ranch Hand
Joined: Feb 24, 2011
Posts: 495
|
|
John Price
|
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” (Mosher's Law of Software Engineering)
“If debugging is the process of removing bugs, then programming must be the process of putting them in.” (Edsger Dijkstra)
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
@john:syntax error in your for loop!
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19214
|
|
|
If you only want to check for the presence, instead of also retrieving the value, you can use a regular expression in combination with String.matches. Check out the Javadoc page of java.util.regex.Pattern for more information. Don't forget to let your regex end with ".*", because String.matches looks for a match of the entire String.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Jack Bush
Ranch Hand
Joined: Oct 20, 2006
Posts: 235
|
|
Hi Rob,
String.matches("(?:apple|orange|grape).*")
works as suggested.
Thank you,
Jack
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19214
|
|
|
You're welcome.
|
 |
 |
|
|
subject: Does String.startsWith() support multiple string?
|
|
|