[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Implementing a wildcard search in a Vector or string array

Posted on 2003-11-06
10
Medium Priority
?
634 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 60 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

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses

650 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