Java Collection Split splitAndKeepEscapedSpaces(String string, boolean preserveEscapes, Collection into)

Here you can find the source of splitAndKeepEscapedSpaces(String string, boolean preserveEscapes, Collection into)

Description

split And Keep Escaped Spaces

License

Open Source License

Declaration

private static void splitAndKeepEscapedSpaces(String string, boolean preserveEscapes, Collection<String> into) 

Method Source Code

//package com.java2s;
/*//from   w  ww. j  a  v  a 2s  .  co  m
 * Copyright (c) 2002-2016 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Collection;

public class Main {
    private static void splitAndKeepEscapedSpaces(String string, boolean preserveEscapes, Collection<String> into) {
        StringBuilder current = new StringBuilder();
        for (int i = 0; i < string.length(); i++) {
            char ch = string.charAt(i);
            if (ch == ' ') {
                boolean isGluedSpace = i > 0 && string.charAt(i - 1) == '\\';
                if (!isGluedSpace) {
                    into.add(current.toString());
                    current = new StringBuilder();
                    continue;
                }
            }

            if (preserveEscapes || ch != '\\') {
                current.append(ch);
            }
        }
        if (current.length() > 0) {
            into.add(current.toString());
        }
    }
}

Related

  1. split(Collection d, int n)
  2. split(Collection collections, String separator)
  3. split(Collection orig, int batchSize)
  4. split(Collection set, int n)
  5. split(final Collection collection)
  6. splitToCollection(Collection collection, String valueString)
  7. splitToCollection(String list, String separator, Collection dest)
  8. splitToCollection(String str, String sep, Collection c)