tags:

views:

178

answers:

7

Hi,

As per my project I need to devide a string into two parts.

below is the example:

String searchFilter = "(first=sam*)(last=joy*)";

Where searchFilter is a string. I want to split above string to two parts

first=sam* and last=joy* so that i can again split this variables into first,sam*,last and joy* as per my requirement.

I dont have much hands on experience in java. Can anyone help me to achieve this one. It will be very helpfull.

Thanks in advance

+1  A: 

Take a look at String.split(..) and String.substring(..), using them you should be able to achieve what you are looking for.

fish
+1  A: 

you can do this using split or substring or using StringTokenizer.

MAS1
A: 

This (untested snippet) could do it:

 String[] properties = searchFilter.replaceAll("(", "").split("\)");
 for (String property:properties) {
   if (!property.equals("")) {
      String[] parts = property.split("=");
      // some method to store the filter properties
      storeKeyValue(parts[0], parts[1]); 
   }
 }

The idea behind: First we get rid of the brackets, replacing the opening brackets and using the closing brackets as a split point for the filter properties. The resulting array includes the String {"first=sam*","last=joy*",""} (the empty String is a guess - can't test it here). Then for each property we split again on "=" to get the key/value pairs.

Andreas_D
All JVM's I know of evaluate `property != ""` to false (as posted in the snippet above).
Bart Kiers
As I said - untested - I was unsure if the first split adds an empty String to the array, because the String ends with a ")". If this is not the case, the test you mentioned is obsolete.
Andreas_D
@Andreas_D, what I meant was that it **always** returns false (on all modern JVM's I'm familiar with). You'll want to use `equals(...)` instead.
Bart Kiers
@Bart K. damn it, stupid mistake, need more coffee ;-))
Andreas_D
@Andreas_D, make that a double espresso! :)
Bart Kiers
+1  A: 

I think you can do it in a lot of different ways, it dependes on you. Using regexp or what else look at http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html

Anyway I suggest:

int separatorIndex = searchFilter.indexOf(")(");
String filterFirst = searchFilter.substring(1,separatorIndex);
String filterLast = searchFilter.substring(separatorIndex+1,searchFilter.length-1);
ungarida
+8  A: 

The most flexible way is probably to do it with regular expressions:

import java.util.regex.*;

public class Test {
    public static void main(String[] args) {

        // Create a regular expression pattern
        Pattern spec = Pattern.compile("\\((.*?)=(.*?)\\)");

        // Get a matcher for the searchFilter
        String searchFilter = "(first=sam*)(last=joy*)";
        Matcher m = spec.matcher(searchFilter);

        // While a "abc=xyz" pattern can be found...
        while (m.find())
            // ...print "abc" equals "xyz"
            System.out.println("\""+m.group(1)+"\" equals \""+m.group(2)+"\"");
    }
}

Output:

"first" equals "sam*"
"last" equals "joy*"
aioobe
A: 

I recommend aioobe answer, i cant vote because of low rep.

The regex is a very powerful tool for working with string. i recommend getting familiar with it.

SnapDragon
+1  A: 

I have a small code that will solve ur problem

StringTokenizer st = new StringTokenizer(searchFilter, "(||)||=");
        while(st.hasMoreTokens()){
            System.out.println(st.nextToken());
        }

It will give the result you want.

Shashank T