Detect if String has "." and how many characters after it??

How do I detect if String has "." and how many characters after it??

For example :

1234.123

would tell me it has a period and has 3 characters after the period. Can be done in multiple step.
894359Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
int dot = s.indexOf(".");
int after = s.length() - dot - 1;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sciuriwareCommented:
This code fails if there is no dot ...................
It should read:

int dot = s.indexOf(".");
int after = (dot < 0) ? 0 : (s.length() - dot - 1);

If you are after the extension of a filename:
String extension = "";
int dot = s.indexOf('.');
if(dot >= 0)
{
     extension = s.substring(dot);
}

<*>
0
WebstormCommented:
Hi 894359,

If you want a different value when there is no period character ( -> -1 ) and when there is 0 characters after the period ( -> 0 ) :

int dot = s.indexOf('.');
int after = (dot < 0) ? -1 : (s.length() - dot - 1);
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

WebstormCommented:

Also, if you want the file extension, you have to use lastIndexOf instead of indexOf, and detect the last file separator also (for example in   C:\my_directory.ext\file_no_extension ) :

int dot=s.lastIndexOf('.'),
     idx1=s.lastIndexOf(java.io.File.separatorChar); // or '/'

if ((idx1>=0)&&(idx1>dot)) dot=-1; // no dot found after the last file separator

String ext=(dot<0)?"":s.substring(dot+1);
0
Mayank SAssociate Director - Product EngineeringCommented:
>> This code fails if there is no dot ...................

Not necessarily ;-) the value of 'after', if negative, means that the 'dot' was not found so the code doesn't fail :)
0
sciuriwareCommented:
Webstorm, you just copied by answer,
mayankeagle, when there's no dot 'after' should be 0 and not negative.
<*>
0
Mayank SAssociate Director - Product EngineeringCommented:
Actually, on second thought, it would be positivie :) and equal to the length of the String, I guess.
0
WebstormCommented:
>> Webstorm, you just copied by answer
Not true, read it again, there is a difference i mentioned in my comment : -1 if '.' is not found instead of 0
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

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.