Java String Split splitNames(final String string)

Here you can find the source of splitNames(final String string)

Description

Splits qualified names by dots.

License

Open Source License

Parameter

Parameter Description
string qualified name

Return

array of names

Declaration

private static String[] splitNames(final String string) 

Method Source Code

//package com.java2s;
/**// www.java  2s.c o m
 * Copyright 2006 StartNet s.r.o.
 *
 * Distributed under MIT license
 */

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Splits qualified names by dots. If names are quoted then quotes are
     * removed.
     *
     * @param string qualified name
     *
     * @return array of names
     */
    private static String[] splitNames(final String string) {
        if (string.indexOf('"') == -1) {
            return string.split("\\.");
        } else {
            final List<String> strings = new ArrayList<String>(2);
            int startPos = 0;

            while (true) {
                if (string.charAt(startPos) == '"') {
                    final int endPos = string.indexOf('"', startPos + 1);
                    strings.add(string.substring(startPos + 1, endPos));

                    if (endPos + 1 == string.length()) {
                        break;
                    } else if (string.charAt(endPos + 1) == '.') {
                        startPos = endPos + 2;
                    } else {
                        startPos = endPos + 1;
                    }
                } else {
                    final int endPos = string.indexOf('.', startPos);

                    if (endPos == -1) {
                        strings.add(string.substring(startPos));
                        break;
                    } else {
                        strings.add(string.substring(startPos, endPos));
                        startPos = endPos + 1;
                    }
                }
            }

            return strings.toArray(new String[strings.size()]);
        }
    }
}

Related

  1. splitLongUnicodeString(String message, List result)
  2. splitMacros(String s)
  3. splitManagedBean(String el)
  4. splitMultiMessageString( String multiMessageString, String messageSplitter)
  5. splitName(String name)
  6. splitObjectString(String str)
  7. splitOgnl(String ognl)
  8. splitOn(String toSplit, String splitter)
  9. splitOnNoWiki(String s)