Checking for a Character in String

I have file names in a folder like:
00001.tif, 00002.tif, MS001.tif, AX002.tif etc

I am reading files names using File API...what I want is I want to check if there is any alphabet in file name...
is there any straight forward method on String or String buffer to check that.
jaipur07Asked:
Who is Participating?
 
CEHJCommented:
Long.parseLong(fileName) will fail is there are letters
0
 
CEHJCommented:
Typo:

is there=if there
0
 
colr__Commented:
Note that with CEHJ 's example,. you'd have to remove the extension first.

Another method would be to do the following:

for (int i=0; i<fileName.size(); i++){
      if (!Character.isDigit(fileName.charAt(i)))
            // non-digit character found, so filename contains a letter (or special character)
}

Not as pretty as CEHJ's though.

colr__
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
CEHJCommented:
Given no numbers in the extension, you could do

boolean valid = fileName.matches("\\d+\\.\\D+");
0
 
objectsCommented:
try something like:

if (Pattern.matches(".*?\\d.*", filename)) {
   // contains a number
}
0
 
WebstormCommented:
>> if there is any alphabet in file name

if (Pattern.matches("[^.]*[A-Za-z].*", filename))
0
 
Nguyen Huu PhuocSenior ManagerCommented:
I think the filename is very complicate but easy as you thought
Take example
0001.003.005.tif
They have many dots.
Your pattern just focus on the filename with one dot.
Phuoc
0
 
WebstormCommented:
then
if (Pattern.matches(".*[A-Za-z].*\\..*", filename)         // accepting multiple dots
  ||Pattern.matches("[^.]*[A-Za-z][^.]*", filename))    // no dot in the file name
0
 
CEHJCommented:
boolean valid = fileName.matches("[\\d.]+\\w+");
0
 
Mayank SAssociate Director - Product EngineeringCommented:
What's the reason, GranMod?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.