• Post Reply Bookmark Topic Watch Topic
  • New Topic

Strings  RSS feed

 
maha devan
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • 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
 
Jason Fox
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • 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/
 
Herb Schildt
Author
Ranch Hand
Posts: 253
6
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • 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 ]
 
Aurelian Tutuianu
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 253
6
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!