File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes special characters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "special characters" Watch "special characters" New topic
Author

special characters

prajakta patil
Ranch Hand

Joined: Dec 11, 2005
Posts: 30
Hi
I want to validate whether the value passed in as a String() is a alphanumeric v. special char value. Can you please tell me how to check that?

Thanks.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
Use the methods of the Character class to examine the chars in the String one by one.
prajakta patil
Ranch Hand

Joined: Dec 11, 2005
Posts: 30
Hi Norm,
I think it is good idea but I want something in built fast method.

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
Originally posted by prajakta patil:
Hi Norm,
I think it is good idea but I want something in built fast method.

Thanks
Use the methods of the Character class to examine the chars in the String one by one.

Alternatively use a regular expression, maybe something like "\\w+"
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
If you want to write your own code, it would depend on the character set.
For example if your character set were ASCII (ie values range from 0 to 255)
you could create an 256 byte array with the contents of the array elements set according to whether or not the corresponding char is in the set of characters that you want. Then use the chars in the String to index into the array to test if the char is in the desired set of chars.

I used this technique to uppercase ASCII strings to normalize searches and found a 20% speed improvement over using the String.toUpperCase() method.
prajakta patil
Ranch Hand

Joined: Dec 11, 2005
Posts: 30
Thank You very much guys,

I will first try to use regex / pattern classes. but for that I will need to do some more research about how to use those classes. If research takes more time then I will use Character array option.

Thanks.
prajakta patil
Ranch Hand

Joined: Dec 11, 2005
Posts: 30
can somebody please tell me how to use regex?


Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
I posted a link to part of the Java Tutorials earlier. Have you read that and its enclosing section?
prajakta patil
Ranch Hand

Joined: Dec 11, 2005
Posts: 30
Thanks Cambell, just now I read that link and I am using following code to check the special characters in string:

final String silly = "star & moon $ 012";
final String[] myArray = silly.split("\\p{Punct}");
System.out.println(myArray.length);
for(final String token : myArray) {
System.out.println(token);
}

OUTPUT:
3
star
moon
012

Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
That seems to be working-well done.
 
jQuery in Action, 2nd edition
 
subject: special characters