简体   繁体   中英

Split String based on Brackets and Comma (Java)

I'm looking for a solution that can convert my string bases on square bracker and commas, and replace bracket with (.) dot. Basically I need this to query database.

Input:

parent[child1[subchild1,subchild2], child2[subchild1,subchild2]]

Output:

parent.child1.subchild1
parent.child1.subchild2
parent.child2.subchild1
parent.child2.subchild2

This would work well with recursion because the structure of the String is a tree. I would write a method like this:

/**
 * Returns an array of database query values for a tree rooted at the given
 * index of a tree (formatted as a String: e.g. the String "A[B[C,D],E]"
 * would return the String array < "A.B.C", "A.B.D", "A.E" >).
 * 
 * Requires a correctly tree-formatted String and an index pointing to neither
 * a comma nor a bracket, and the List parameter must point to an empty List.
 * (Behavior is undefined if these conditions are not met.)
 *
 * @param codedTree      the coded tree to search
 * @param index          the index of the tree to start from
 * @param queries        your result: all possible node1.node2... coded subtrees
 * @return               the index of the closing bracket for this tree
 */ 
private static int treeStringToQuery(final String codedTree, final int initialIndex,
        final List<StringBuilder> queries) {

    int currentIndex = initialIndex;

    // Add a loop here to set currentIndex to index of first '['.

    final String prefix = codedTree.substring(initialIndex, currentIndex);

    // Point index to start of first subtree.
    currentIndex++;
    
    // Add a loop here until currentIndex is pointing at a right bracket.
    // Each iteration, currentIndex will be pointing at start of a subtree.

        final List<StringBuilder> subQueries = new ArrayList<>();

        // Inside the loop, recurse. Then set currentIndex to the return
        // value + 1. Add prefix to every StringBuilder object in subQueries.
        // Finally, append subQueries to the end of queries.

    // End loop. Now queries is complete, and currentIndex is correct.
    return currentIndex;
}

The basic idea is to start from the given index and save the substring word you find at that index. Then loop to get each subtree. For each subtree, simply recurse to get its queries and the index of the next subtree (or else the matching ']' if there are no more). Add your prefix to the start of each string in the sublist, then append the entire sublist to your list (the 'queries' parameter). When you're done with the loop, currentIndex should be pointing at the matching ']'.

Let me know if you follow the logic or if I made an error. It's actually pretty straightforward if you understand recursion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM