Solved

Implementing a wildcard search in a Vector or string array

Posted on 2003-11-06
10
623 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
You probably have too low a version of the compiler/runtime. You need SDK 1.4.x
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 step-by-step guide to install VisualVM launcher in eclipse.

771 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now