|
||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionHave you ever searched for files and folders in Windows operating system? Generally most of the users try to search various .doc files, .pdf files etc. We generally search by giving *.doc or *.pdf in the text box of the search window of the windows OS. Of course windows OS provides some other more specific search utilities. However this kind of search is used in many applications. If an application provides this kind of facility it is considered as the best feature of that application. BackgroundIt is always important for an application how the user is going to provide a search condition. All kinds of search conditions are not always feasible. Generally It has been universally endorsed that a user can search based upon certain specifications. As far this specification is concerned, we generally search by providing wild card characters. The wild card characters are * and ? . There is a concept behind these characters. I hope you know about it. This kind of search specification is recurrent in various applications. If you are working on an application, there will be a text field where user has to search the name of the persons by giving * or ? or a combination both and alphanumeric characters. For example I have to search a name, I can give J* so that my out may be “John”, “Johnson” or anything that starts with “J”. There is another case that I have to search exactly four characters, so I can give my search condition as ???? . However user can give any kind of combinations to search the String pattern. In case of Java based application you can achieve this feature by using regular expression. But the formation of regular expression depends upon you. Sometimes we may find it difficult to form the exact regular expression to meet the expectation. However if the requirement is only to find out the match a String based upon the wild card pattern, I have provided an example below. You can use anywhere where you want to search a name based upon the wild card pattern. Using the codeThe following class is a utility class where you can use as string finder for wild card pattern. For example you have your original String as “John” and you want to find whether the original string matches with the wild pattern as “J*”. You can do it easily. I have provided the test harness class to test the functionality. You can test in various ways. package com.ddlabs.core.util; import java.util.Vector; /**This class is a utility for finding * the String based upon the wild card * pattern. For example if the actual * String "John" and your wild card pattern * is "J*", it will return true. * @author Debadatta Mishra(PIKU) * */ public class WildCardStringFinder { /** * String variable for wild card pattern */ private String wildCardPatternString; /** * Variable for the length of the wild card pattern */ private int wildCardPatternLength; /** * Boolean variable to for checking wild cards, * It is false by default. */ private boolean ignoreWildCards; /** * Boolean variable to know whether the pattern * has leading * or not. */ private boolean hasLeadingStar; /** * Boolean variable to know whether the pattern * has * at the end. */ private boolean hasTrailingStar; /** * A String array to contain chars */ private String charSegments[]; /** * Variable to maintain the boundary of the String. */ private int charBound; /** * Default constructor */ public WildCardStringFinder() { super(); ignoreWildCards = false; } /**This is the public method which will be called to match a String * with the wild card pattern. * @param actualString of type String indicating the String to be matched * @param wildCardString of type String indicating the wild card String * @return true if matches */ public boolean isStringMatching( String actualString , String wildCardString ) { wildCardPatternString = wildCardString; wildCardPatternLength = wildCardString.length(); setWildCards(); return doesMatch( actualString, 0, actualString.length() ); } /** * This method is used to set the wild cards. * The pattern for the wild card may be *, ? or * a combination of *,? and alphanumeric character. */ private void setWildCards() { if(wildCardPatternString.startsWith("*")) { hasLeadingStar = true; } if (wildCardPatternString.endsWith("*") && wildCardPatternLength > 1 ) { hasTrailingStar = true; } Vector
Points of InterestTo test the above programs, please create the appropriate package as mentioned in the program. You can also create your own package and modify the package name in the above programs. You can have all the code in your favorable java editor. I hope that you will enjoy my article. If you find any problems or errors, please feel free to send me a mail in the address debadattamishra@aol.com . This article is only meant for those who are new to java development. This article does not bear any commercial significance. Please provide me the feedback about this article.
|
|||||||||||||||||||||||||||||||||||||||||||||||