Java String Split split(String text)

Here you can find the source of split(String text)

Description

split

License

Open Source License

Declaration

private static List<String> split(String text) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    private static List<String> split(String text) {
        final ArrayList<String> result = new ArrayList<>();
        String newText = "";
        Scanner scan = new Scanner(text).useDelimiter(" ");
        while (scan.hasNext()) {
            newText += scan.next();/* ww w  .  java  2 s  .com*/
            final int openingIdx = newText.indexOf("(");
            if (openingIdx == -1) {
                result.add(newText);
                newText = "";
            } else {
                final int closingIdx = indexOfClosingBracket(newText, openingIdx);
                if (closingIdx == -1) {
                    newText += " ";
                } else {
                    result.add(newText);
                    newText = "";
                }
            }
        }
        return result;
    }

    private static int indexOfClosingBracket(String text, int openingBracket) {
        int result = -1;
        ArrayDeque<String> stack = new ArrayDeque<>();
        for (int i = openingBracket; i < text.length(); i++) {
            if (text.charAt(i) == '(') {
                stack.push("(");
            } else if (text.charAt(i) == ')') {
                if (stack.isEmpty()) {
                    break;
                }
                stack.pop();
                if (stack.isEmpty()) {
                    result = i;
                    break;
                }
            }
        }
        return result;
    }
}

Related

  1. split(String string)
  2. split(String string)
  3. split(String string, String seperator)
  4. split(String strSource, String strReg)
  5. split(String text)
  6. split(String text)
  7. split(String text, String token)
  8. split(String token, String s)
  9. split(String toSplit)