Java String Split splitStep(String step)

Here you can find the source of splitStep(String step)

Description

Splits the specified step into parts.

License

BSD License

Parameter

Parameter Description
step the step.

Return

the specified step in parts.

Declaration

public static String[] splitStep(String step) 

Method Source Code


//package com.java2s;
/*//from w w  w . j  a v a2s .com
 * Copyright (c) 2012. betterFORM Project - http://www.betterform.de
 * Licensed under the terms of BSD License
 */

import java.util.ArrayList;

public class Main {
    /**
     * Splits the specified step into parts.
     * <p/>
     * Examples:
     * <ul>
     * <li><code>"a"</code> returns <code>{"a"}</code>.
     * <li><code>"a[p]"</code> returns <code>{"a", "p"}</code>.
     * <li><code>"a[p][q]"</code> returns <code>{"a", "p", "q"}</code>.
     * <li><code>"child::a[p][q]"</code> returns <code>{"child::a", "p", "q"}</code>.
     * <li><code>"a[b[p]][c/d[q]]"</code> returns <code>{"a", "b[p]", "c/d[q]"}</code>.
     * </ul>
     *
     * @param step the step.
     * @return the specified step in parts.
     */
    public static String[] splitStep(String step) {
        if (step == null) {
            return null;
        }

        int offset = 0;
        int braces = 0;
        ArrayList list = new ArrayList();
        for (int index = 0; index < step.length(); index++) {
            switch (step.charAt(index)) {
            case '[':
                if (braces == 0) {
                    if (offset == 0) {
                        list.add(step.substring(offset, index));
                    }
                    offset = index + 1;
                }
                braces++;
                break;
            case ']':
                if (braces == 1) {
                    list.add(step.substring(offset, index));
                    offset = index + 1;
                }
                braces--;
                break;
            }
        }

        if (offset < step.length()) {
            list.add(step.substring(offset, step.length()));
        }

        return (String[]) list.toArray(new String[0]);
    }
}

Related

  1. splitSentence(String sentence)
  2. splitSimple(String split, String s)
  3. splitSimpleQueries(String queryString)
  4. splitSQL(List list, String sql)
  5. splitSQLColumns(String sql)
  6. splitStr(String str, int toCount)
  7. splitString(String bigString, String splitter)
  8. splitString(String hexString, int partSize)
  9. splitString(String s)