Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Finding Numeric value from String object  RSS feed

 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to find from a String object does it has numeric value in it.
for eg:
String vName = "Test" --> it should return no numeric value
String vName = "Test123"--> it should return it has numeric value

Thanks,

aakash
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Straight forward method to do this is

I think you can also use regular expressions, but I'm not sure how to use it.
 
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using regular expressions:



Check out the java.util.regex package for more details on the Pattern and Matcher classes.
 
aakash bhatt
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Many and Jeffrey for the solution.

On performance criteria which one should I use.
 
Jeffrey Hunter
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The regular expression route does seem a bit intense for a simple query such as yours (does a digit appear in this string?). The regex classes are optimized to find patterns in strings, which is in the same problem neighborhood as yours, but still different enough to question what the best approach would be.

As far as your problem is concerned, you can exit the loop iteration as soon as you find the first occurrence of a digit, so I would use the loop, and charAt() technique. It works in constant time, and you do not have the overhead incurred when instantiating the regex classes.
 
Jeffrey Hunter
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, as I'm in a particularly anal-retentive mood, I went and checked the two techniques and found that the loop with charAt() method indeed runs faster than using the regular expression technique when attempting to find a digit within a String.
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff - did you try something like this?

This makes the pattern compilation a one-time thing. Also note that the regex need find only one digit, just like the charAt() method. The charAt() method will probably still be slightly faster, but the difference will be much less noticeable this way, I think...
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving all this fast talk to the Performance forum...
 
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well - I have some testcases:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!