Java String Split splitHelperName(String name)

Here you can find the source of splitHelperName(String name)

Description

This implementation is quite naive and should be possibly rewritten.

License

Apache License

Parameter

Parameter Description
name a parameter

Declaration

static Iterator<String> splitHelperName(String name) 

Method Source Code

//package com.java2s;
/*/*w  ww  .ja v a  2  s. com*/
 * Copyright 2013 Martin Kouba
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.ArrayList;

import java.util.Iterator;
import java.util.List;

public class Main {
    /**
     * This implementation is quite naive and should be possibly rewritten.
     *
     * @param name
     * @return
     */
    static Iterator<String> splitHelperName(String name) {

        boolean literal = false;
        boolean whitespace = false;
        List<String> parts = new ArrayList<String>();
        StringBuilder buffer = new StringBuilder();

        for (int i = 0; i < name.length(); i++) {
            if (name.charAt(i) == ' ') {
                if (!whitespace) {
                    if (!literal) {
                        parts.add(buffer.toString());
                        buffer = new StringBuilder();
                        whitespace = true;
                    } else {
                        buffer.append(name.charAt(i));
                    }
                }
            } else {
                if (name.charAt(i) == '"') {
                    if (literal) {
                        literal = false;
                    } else {
                        literal = true;
                    }
                }
                whitespace = false;
                buffer.append(name.charAt(i));
            }
        }

        if (buffer.length() > 0) {
            parts.add(buffer.toString());
        }
        return parts.iterator();
    }
}

Related

  1. splitElements(String values)
  2. splitEx(String str, String spilter)
  3. splitFields(String fieldsString, int minNum)
  4. splitForIndexMatching(String string)
  5. splitGenericIfNeeded(String name)
  6. splitHistory(String history)
  7. splitHTMLTags(final String string)
  8. splitIdKeyConfig(String config)
  9. splitInitialItems(final String text)