Solved

Implementing a wildcard search in a Vector or string array

Posted on 2003-11-06
10
630 Views
Last Modified: 2013-12-14
hi gurus

i need to implement a wildcard search in a vector and a string array.
If I have a string array with userids:
             String [] userid = {"danG", "thomasG", "davidsK"};

If I do a search on da* then the returned values I want in a string[] or vector
is {"danG", "davidsK"}

how would this work for both vector and string[]
thank-u in advance
SB
0
Comment
Question by:ScarletBlue
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9693435
You can use regular expressions for this. Or for that simple example a substring function would suffice. If you specify the function prototype like this:

public List getMatches(Object o, String toMatch) {
    List source = null;
    if (o instanceof  String[]) {
        source = Arrays.toList(o);
    }
    // now iterate, matching
     
}

you can cope with either Vector or String[]
0
 
LVL 2

Expert Comment

by:Tols
ID: 9693483
It can be done in many ways.

String class offers many methods for finding substrings, comparing etc. Simple create methot for searching strings matching to mask.
0
 

Author Comment

by:ScarletBlue
ID: 9693852
Thank u both for your help..thou I am still abit stuck.
Tols I have used your advice and went with String methods.

String [] userid = {"danG", "thomasG", "davidsK"}
String  inputFromUser = "da*"
String[] revisedUsers = null;

for(int i=0; i<userid; i++){
     String x = null;
     if(x.startsWith(inputFromUser)){
         revisedUsers = //HOW DO I ADD THE STRING VALUES TO THIS ARRAY
         response.write(userid[i]);
     }
}

-----------
How do i get rid of the * so that it only searches for strings begining with "da"
and how do I add those values to a string array?




0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 86

Expert Comment

by:CEHJ
ID: 9693887
>>How do i get rid of the * so that it only searches for strings begining with "da"

You can't without using regular expressions [unless you just do s.substring("da")], but of course it will then match 'Dada' too.

Using arrays is also not appropriate as arrays are inherently fixed-size, whereas you need a dynamic structure.
0
 
LVL 4

Expert Comment

by:kalosi
ID: 9694027
Hi,

you can also use regular expressions to match the string. in this case you don't need to get rid of the * character.

The String.match(String regex) method is the same as the Pattern.matches(regex, str) call.


String [] userid = {"danG", "thomasG", "davidsK"};
String inputFromUser = "da*";

Vector revisedUsers = new Vector();

for (int cc = 0; cc < userid.length; cc++)
{
  try
  {
   if (userid[cc].matches(inputFromUser))
      revisedUsers.add(userid[cc]);
   } catch (PatternSyntaxException  ex) { // may be thrown on bad pattern.
      ex.printStackTrace();
   }
}


david
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9694031
>>but of course it will then match 'Dada' too.

Unless of course you call s.startWith("da") in which case it will not 'armada', but will match 'dad'
0
 
LVL 4

Expert Comment

by:kalosi
ID: 9694067
If your search string is only searching for prefixies you can rewrite the entered userInput from da* to ^da*. So the string must start with "da" followed by anything .

david
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9696626
>>you can also use regular expressions to match the string. in this case you don't need to get rid of the * character.

'*' has quite a distinct meaning in terms of regular expressions - and it's not what you probably think it is - as the following demonstrates. It means zero or more of the preceding character in this case. Regular expressions are very powerful if you know how to use them and if you want to do this sort of thing, it's the way to go.


import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Wildcard {

      public static void main(String[] args) {
            String s1 = "drone";
            final String regex = "da*";

            Pattern pat = Pattern.compile(regex);
            Matcher mat = pat.matcher(s1);
            if (mat.find()) {
                  System.out.println("Matcher did find for String " + s1 + " at index " + mat.start());
            }
            else {
                  System.out.println("Matcher did not find for String " + s1);
            }
            // A 'match' operation applies to the whole String
            s1 = "d";
            mat = pat.matcher(s1);
            if (mat.matches()) {
                  System.out.println("Matcher did match String " + s1);
            }
            else {
                  System.out.println("Matcher did not match String " + s1);
            }

      }

}
0
 

Author Comment

by:ScarletBlue
ID: 9699369
Hi

For some reason I cannot use java.util.regex.Pattern or java.util.regex.Matcher.
I am using Eclipse developing in a Portal Development environment.

I need to have a jar file of these classes to use it in Eclipse. Where can I find these classes?

Thank-u for everyone's help
SB
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 20 total points
ID: 9699751
You probably have too low a version of the compiler/runtime. You need SDK 1.4.x
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
web services creation SOAP vs REST 5 56
Windows 10 IE Certificate Issue 10 51
jsp insert to database example 2 44
going to wrong jsp page 2 33
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question