• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strings

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I am having a String I want to know whether that string contains any number in it .how to find that..

For example String "essdds" doesn't have any number whereas "asds123" has.

Thanks
Mahesh
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depending on what JVM you're writing for, you may be able to use the java regular expressions library. There's a tutorial over at:

http://java.sun.com/docs/books/tutorial/extra/regex/
 
Author
Posts: 253
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maha:

Since you are a beginner, I thought it would be useful to show a simple way to see if a string contains a digit. Although there are more efficient methods of accomplishing this task, the following code is easy to understand and will give you a starting point.

This approach simply examines each character in a string by using the charAt method of String. It then tests that character by using the isDigit method of Character. If a digit is found, the loop stop early. If a loop completes, it means that no digit was found. The output is shown here.

str has no digits
str2 has digits

This will get you started, but I am sure that you can do better.
 
maha devan
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mr Schildt

I am having Complete reference of urs. Its nice and excited to get a reply from you personally.
U had mentioned that other efficient methods are there for this case, can u provide those methods also . I think I will be able to understand those.

Thanks
Mahadevan
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another possible solution would use something like


Note I haven't tested this, but something along these lines should work.
[ May 14, 2004: Message edited by: Jeroen Wenting ]
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maha:

Simple example of the same function, but with regular expression



where the regular expression "[^0-9]*" can be "translated" like this :

any character (last '*') must be in the set (i.e. '[' and ']') of all characters except (i.e. '^') of characters from '0' to '9' ('0-9').

all the best.
 
Herb Schildt
Author
Posts: 253
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maha:

Two improvements come quickly to mind.

The first is the approach described by Aurelian, above. The regular expression mechanism is a powerful feature that streamlines solutions to many types of problems.

Another approach is to convert the string into a character array by calling String's toCharArray method. You can then index the array using the normal indexing syntax (i.e., str[i]). This has the advantage of eliminating the overhead of the repeated calls to charAt.
 
I think he's gonna try to grab my monkey. Do we have a monkey outfit for this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic