• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1191
  • Last Modified:

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.
0
jaipur07
Asked:
jaipur07
5 Solutions
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
phuocnhCommented:
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now