• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding Numeric value from String object

 
aakash bhatt
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
 
Mani Ram
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.
 
Jeffrey Hunter
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.
 
Jim Yingst
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...
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving all this fast talk to the Performance forum...
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well - I have some testcases:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!