Re: indexOf
On Thu, 21 Feb 2008 06:17:28 -0500, "Jean Pierre Daviau"
<Once@WasEno.ugh> wrote, quoted or indirectly quoted someone who said
:
public static boolean checkExtensions(String tmp){
String ext[] = {"html", "htm", "lnk", "mov", "avi", "psd", "ai",
"ps", "tif", "nws", "txt", "raw", "pdf"};
for (int i=0; i<ext.length ;i++ )
{
if(tmp.indexOf(ext[i]) != -1){
return true;
}
}
return false;
}
Some improvements:
1. your list of extensions can be static final. No need to build the
list on every call.
2. you can use for:each syntax on your loop. see
http://mindprod.com/jgloss/jcheat.html
3. I would compare >=0 rather than != -1. Comparing against 0 is
faster than against other integers. It also guards against a freaky
-2. It also more conventional, and hence easier for other programmers
to read.
4. you don't really want indexOf. file "external.doc" would pass
your "ext" check. You want endsWith including a dot.
See http://mindprod.com/products1.html#FILTER. It has
source code for a extensions filter that does just this for you.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com