Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

How to check whether the input string contains numbers,comma,colon

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,
In my project i want to check whether the user's input string contains numbers,commma,colon,double and single quotes and also any special characters
Iam getting the user input as a string.
Can anyone tell me how to check for this?

Thanks a lot.
 
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Refer Regex package along with tutorial

The pattern expression would be something like this : [^a-zA-Z]

i.e. any character other than a to z and A to Z
 
Santhana Lakshmi.S
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
Thanks a lot for your reply.
If you dont mind can you explain it with an example or sample code.
Thanks a lot
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Java In General (the best place to ask about string handling questions).
 
Santhana Lakshmi.S
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
If i java.util.regex class i need to give the pattern where the special characters will occur.Am i right?
But in my case, the input string is user defined one.
In that,at any place the special characters and numbers will occur.
How to check those?
Thanks in advance
 
Rajah Nagur
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I can repharse your requirement, it will be " To check whether the user input contains anything else other than a to z or A to Z characters"

Here anything else are numbers,colon,double,special characters etc

So you create a pattern to get all the matches that does not contain a-z or A-Z. After you compile the pattern, the matches should return only those values that do not contain a-z / A-Z which will be special chars, numbers, colons etc.

So I suggested the pattern would be [^a-zA-Z]
^ symbol is not negation.

For concrete examples refer tutorial link / API reference
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Santhana Lakshmi.S:
hi
If i java.util.regex class i need to give the pattern where the special characters will occur.Am i right?
But in my case, the input string is user defined one.
In that,at any place the special characters and numbers will occur.
How to check those?
Thanks in advance



Regular expression syntax allows you to specify the characters without worrying about their exact placement in the string. For instance the pattern [a-z]* matches zero or more lower case letters. It doesn't matter what order the letters are in.
 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do a switch statement...

like:



then you could,


[ July 17, 2006: Message edited by: Justin Fox ]
 
See where your hand is? Not there. It's next to this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic