File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
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: 19654
    
  18

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 - OCEEJBD 6
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: 19654
    
  18

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Does String.startsWith() support multiple string?
 
Similar Threads
WA #1.....word association
Betwixt Problem...
Show last value in the html:select
Alphabets
Counting in arrays