Hello all I have a string Im getting back from the database as follows:

('MCKINNEY','TX'),('ALLEN','TX'),('ANNA','TX')

I need to break it down and drop into an array
'MCKINNEY','TX'
'ALLEN','TX'
'ANNA','TX'

..normally I get multiple records and just drop the resultSet into a Vector and do "elementAt(i)" to display, but I never messed with a string like so

any suggestions?

I was thinking using replaceAll

for (String s : input.split("),(")) {
     System.out.println(s.repalceAll("(", "").replaceAll(")", ""));
}

that dont work...

so you want to parse out the six strings? hmm try this:

bool onefound=false;
int count=0;
int first;

for( int i=0; i<thestring.length(); i++)
{
if (thestring.at(i)=='\'';
{

if (onefound)
{
thearray[count]= thestring.substring(first,i);
i++;
onefound=false;
}
else
{
onfound=true;
first=i;
}
else

}

I just wrote that out, it probably need cleaning up, but the idea's there. There might be a nice regex way too. :D

You can use regular expressions. I'm not good with them since I hardly use them, but they aren't that bad, just a set of rules to specify which characters to match basically. Look up regex or regular expressions java on google.

Please try following function, hope it will work for you.

/**
*This method accepts the input in the form of ('AAA','CC'),('DDD','D')
* and returns the array of String contains content of braces.
*/
    private String[] splitString(String mainString) {
        String[] tempString = mainString.split("[)]");
        for( int i = 0 ; i < tempString.length ; i++ ) {
            tempString[i] = tempString[i].substring(tempString[i].indexOf("(")+1);
            System.out.println(tempString[i]);
        }
        return tempString;
    }

Edited 3 Years Ago by Reverend Jim: Fixed formatting

hi mimsc,
spliting the string using the regex.

String strInput="('MCKINNEY','TX'),('ALLEN','TX'),('ANNA','TX')";
String strProcess=null;
String strOutputs[]=null;
if(strInput!=null){
    strProcess=strInput.substring(1,strInput.length()-1);
    System.out.println(strProcess);
    strOutputs=strProcess.split("\\),\\(");
}

strOutputs is the output array...

Edited 3 Years Ago by Reverend Jim: Fixed formatting

A simple way would be to remove all opening brackets "(" in your String and then split on ")," to get the desired result.

Another way would be to use the Pattern and Matcher object to do the same job.

Pattern p = Pattern.compile("\\(([^)]+)\\)");
Matcher m = p.matcher(s);
while(m.find()) {
    // Group 0 would be the entire string matched ('A', 'B')
    // and hence we are using group(1) which would give us
    // 'A', 'B' as desired
    System.out.println(m.group(1));
}

thank you all for the suggestions..I just got in and will try them out...good day to you all!!

Marking this thread as solved since you already started a new thread which is a continuation of this one.

This question has already been answered. Start a new discussion instead.