Java String Tokenize tokenizePathAsArray(String path)

Here you can find the source of tokenizePathAsArray(String path)

Description

Same as #tokenizePath tokenizePath but hopefully faster.

License

Open Source License

Declaration

private static String[] tokenizePathAsArray(String path) 

Method Source Code

//package com.java2s;
/*/*from   w  ww.  jav a2s. co  m*/
 * Copyright (c) 2002-2012 Alibaba Group Holding Limited.
 * All rights reserved.
 *
 * 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.
 */

public class Main {
    private static final char FILE_SEP_CHAR = '/';

    /** Same as {@link #tokenizePath tokenizePath} but hopefully faster. */
    private static String[] tokenizePathAsArray(String path) {
        char sep = FILE_SEP_CHAR;
        int start = 0;
        int len = path.length();
        int count = 0;

        for (int pos = 0; pos < len; pos++) {
            if (path.charAt(pos) == sep) {
                if (pos != start) {
                    count++;
                }

                start = pos + 1;
            }
        }

        if (len != start) {
            count++;
        }

        String[] l = new String[count];

        count = 0;
        start = 0;

        for (int pos = 0; pos < len; pos++) {
            if (path.charAt(pos) == sep) {
                if (pos != start) {
                    String tok = path.substring(start, pos);

                    l[count++] = tok;
                }

                start = pos + 1;
            }
        }

        if (len != start) {
            String tok = path.substring(start);

            l[count /* ++ */] = tok;
        }

        return l;
    }
}

Related

  1. tokenizeLine(String line)
  2. tokenizeName(String name)
  3. tokenizeNumbersFromString(String stream)
  4. tokenizeOnWhitespace(String input)
  5. tokenizePath(String path)
  6. tokenizePattern(String pattern)
  7. tokenizeQuotedStrings(final String aInput, final String aDelimiters)
  8. tokenizeQuotes(String f1)
  9. tokenizeStatement(String statement)