aspose file tools
The moose likes Java in General and the fly likes Does String.startsWith() support multiple string? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Does String.startsWith() support multiple string?" Watch "Does String.startsWith() support multiple string?" New topic
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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Does String.startsWith() support multiple string?
 
Similar Threads
Betwixt Problem...
Alphabets
Counting in arrays
Show last value in the html:select
WA #1.....word association