File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Searching a char[]

 
Pan Niko
Ranch Hand
Posts: 86
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Im building a Java programm, that takes an ID. This ID (String) must be 16 characters long and with specific structure:

AN AN AN AN AN N N N N N N AN AN AN A A

AN: Alphanumeric
N: Number (0-9)
A: Alphabetic (A-Z)

Until now im converting the String to charArray, and create another char array that contains letters A-Z but i dont know how to check if characters from 0-4 is letters or numbers etc...

thank you
 
Kurt Van Etten
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't really need to convert the String to a char array, since the String charAt() method allows you to look at the individual characters one by one. In any event, there is a standard programming trick for doing this type of thing: the numeric values for the digits '0' through '9' appear in order in the ASCII sequence, and likewise for the alpha characters 'A' through 'Z'. Since chars can be used like numbers in expressions, this allows you to use <=, =>, etc. in your code to determine if the chars in the ID meet your criteria.

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, what exactly do you want to do; check if a string has the correct format? A regular expression would be the perfect tool for that. For example:

As Kurt says you can get individual characters out of a string using the charAt() method. You can get parts out of the string by using the substring() method. For example, to get a string containing the first alphanumeric part of 5 characters, you could do id.substring(0, 5).
 
Pan Niko
Ranch Hand
Posts: 86
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both for your answers. Jesper de Jong answer looks great, but for your answer Kurt Van Etten how i will check the structure, i will do it one by one character? Because that is not so efficiency.

Thank you

EDIT: My mistake didt spot the last line in Jesper de Jong post
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kurt Van Etten wrote:You don't really need to convert the String to a char array, since the String charAt() method allows you to look at the individual characters one by one. In any event, there is a standard programming trick for doing this type of thing: the numeric values for the digits '0' through '9' appear in order in the ASCII sequence, and likewise for the alpha characters 'A' through 'Z'. Since chars can be used like numbers in expressions, this allows you to use <=, =>, etc. in your code to determine if the chars in the ID meet your criteria.


The problem with that "trick" is that it doesn't work well. That is for non-latin characters. For instance:
 
Kurt Van Etten
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Wouter, I had actually at first written in my response that Pan might want to look at the Character isDigit(), isLetter(), and isLetterOrDigit() methods. I changed what I wrote because it looked like the problem was specifically requiring ASCII character ranges. It's certainly worth being aware of this, though, for situations where you want to allow for correct internationalization.

Pan, you're right that Jesper's approach is better than mine, both in terms of efficiency and conciseness of code, if you're familiar with regular expressions. (Of course, behind the scenes the regular expression matching is still scanning along the input String character by character, but it's likely to be doing it much more efficiently than some routine you've manually coded.)
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic