Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

special characters

 
prajakta patil
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 886
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the methods of the Character class to examine the chars in the String one by one.
 
prajakta patil
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Norm,
I think it is good idea but I want something in built fast method.

Thanks
 
Campbell Ritchie
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 886
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can somebody please tell me how to use regex?


Thanks.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I posted a link to part of the Java Tutorials earlier. Have you read that and its enclosing section?
 
prajakta patil
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That seems to be working-well done.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic