Solved

Implementing a wildcard search in a Vector or string array

Posted on 2003-11-06
10
632 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

717 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