Here you can find the source of split(final String expression, final char separator)
Parameter | Description |
---|---|
expression | The expression to split. |
separator | The separator character. |
public static String[] split(final String expression, final char separator)
//package com.java2s; /*/*from w w w .j a v a 2 s .c om*/ * Copyright (C) 2012 Klaus Reimer <k@ailis.de> * See LICENSE.md for licensing information. */ import java.util.ArrayList; import java.util.List; public class Main { /** * Splits an expression by the specified separator but keeps track of * brackets so separator characters inside of a sub type are ignored. The * splitted parts are also trimmed. * * @param expression * The expression to split. * @param separator * The separator character. * @return The splitted expression parts. */ public static String[] split(final String expression, final char separator) { final List<String> parts = new ArrayList<String>(); int start = 0; int level = 0; final int len = expression.length(); for (int i = 0; i < len; i += 1) { final char c = expression.charAt(i); if (c == separator) { if (level == 0) { parts.add(expression.substring(start, i).trim()); start = i + 1; } } if (c == '{' || c == '<' || c == '[' || c == '(') level += 1; else if (c == '}' || c == '>' || c == ']' || c == ')') level -= 1; } if (start <= len) parts.add(expression.substring(start, len).trim()); return parts.toArray(new String[parts.size()]); } }